Re: stregex question [message #67221] |
Tue, 21 July 2009 09:53  |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
wlandsman wrote:
> I'd like to find the first "e" in a string that is not next to
> another "e"
>
> The solution
> stregex(s,"e[^e]|$") ;"e" followed by not "e" or at the end of the
> line
> doesn't work for the string st = 'see me' because it will find the
> second "e" in "see"
>
> The soluton
> stregex(s,"e{1}") ;Find one occurrence of "e"
> also finds the first "e" in "see" (Two occurrences is also one
> occurrence)
>
> In Perl it seems that one would use the "lookahead" option, but that
> doesn't seem to be available in stregex().
>
> (Actually I am looking for the first quote that is not part of a
> double quote, but I wanted to keep the example syntax simple.)
> Thanks, --Wayne
I would do something like:
IDL> pos = stregex(s, '([^e]e[^e])|(^e[^e])|(^e$)|([^e]e$)')
IDL> pos += strmid(s, pos, 1) ne 'e'
You have to add one to pos if either the [^e]e[^e] or [^e]e$ forms matched.
Mike
--
www.michaelgalloy.com
Associate Research Scientist
Tech-X Corporation
|
|
|
Re: stregex question [message #67463 is a reply to message #67221] |
Tue, 21 July 2009 17:19  |
JDS
Messages: 94 Registered: March 2009
|
Member |
|
|
On Jul 21, 12:53 pm, mgalloy <mgal...@gmail.com> wrote:
> wlandsman wrote:
>> I'd like to find the first "e" in a string that is not next to
>> another "e"
>
>> The solution
>> stregex(s,"e[^e]|$") ;"e" followed by not "e" or at the end of the
>> line
>> doesn't work for the string st = 'see me' because it will find the
>> second "e" in "see"
>
>> The soluton
>> stregex(s,"e{1}") ;Find one occurrence of "e"
>> also finds the first "e" in "see" (Two occurrences is also one
>> occurrence)
>
>> In Perl it seems that one would use the "lookahead" option, but that
>> doesn't seem to be available in stregex().
>
>> (Actually I am looking for the first quote that is not part of a
>> double quote, but I wanted to keep the example syntax simple.)
>> Thanks, --Wayne
>
> I would do something like:
>
> IDL> pos = stregex(s, '([^e]e[^e])|(^e[^e])|(^e$)|([^e]e$)')
> IDL> pos += strmid(s, pos, 1) ne 'e'
>
> You have to add one to pos if either the [^e]e[^e] or [^e]e$ forms matched.
Or, somewhat simpler, and a little easier to maintain:
IDL> pos =(stregex('see me','([^e]|^)(e)([^e]|$)',/SUB))[2]
|
|
|
Re: stregex question [message #67470 is a reply to message #67221] |
Tue, 21 July 2009 10:58  |
wlandsman
Messages: 743 Registered: June 2000
|
Senior Member |
|
|
Mike,
> IDL> pos = stregex(s, '([^e]e[^e])|(^e[^e])|(^e$)|([^e]e$)')
> IDL> pos += strmid(s, pos, 1) ne 'e'
Thanks for the quick reply. For my own reference when I go back to
google this answer, the above code is testing 4 possibilities
[^e]e[^e] - "e" surrounded by non-"e"
(^e[^e]) - "e" at beginning of string followed by non-"e"
(^e$) - Single character string with value "e"
([^e]e$) - - "e" at the end of the string preceded by a non-"e"
In the first and last case (testing a preceding character) one needs
to add 1 to the position found. --Wayne
|
|
|