[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Regular Expressions



Wayne Landsman wrote:
> 
> The following is probably a simple question for anyone familiar with
> regular expressions,  but I am still trying to learn the STREGEX
> function.
> 
> Suppose I want to find the first occurence in a string of an 'l' ithat
> is not part of a double 'l'.   For
> example, in the string
> 
> IDL> st = 'The rolling hills and lake'
> 
> I want to return the character position of the 'l' in lake (=21).
> 
> The following expression almost works -- it will search for any 'l'
> which is both preceded and followed by anything that is not   "l"
> 
> IDL> print,stregex(st, '[^l]l[^l]' )
> 
> but it won't work for the string 'The rolling hills and pool'  because
> the final 'l' has no characters following it.    Any suggestions?

IDL> print, stregex(st,'(^|[^l])l($|[^l])')

which means "a character that is not 'l', or the beginning of the
string, followed by an 'l', followed by a character that is not 'l', or
the end of the string".  Aren't you glad Ken Thompson didn't decide
originally to develop regexps in english?  

This will also work on

IDL> st = "let's all go the the movies"

JD