How to match where the search started?

Thomas Jollans thomas at jollybox.de
Tue Sep 28 16:44:54 CEST 2010


On Tuesday 28 September 2010, it occurred to Florian Kaufmann to exclaim:
> >From the documentation:
> 7.2.4. Regular Expression Objects, search(string[, pos[, endpos]])
> ... the '^' pattern character matches at the real beginning of the
> string and at positions just after a newline, but not necessarily at
> the index where the search is to start....
> 
> But I'd like to do just that. In Emacs regexps, I think the closest
> equivalent would be \=. Then I could do something like that, and also
> find directly adjacent matches
> 
> reo = re.compile( r'(\=|...)...' );
> while True
>   mo = reo.search(text,pos)
>   if not mo: break
>   ...
> 
> Flo

You could prefix your regexp with r'(.*?)' to create a match of stuff that is 
between the start of search and the start of the first thing you're interested 
in. (untested...)



More information about the Python-list mailing list