searching backwards in a string
Jason Orendorff
jason at jorendorff.com
Tue Feb 12 12:09:05 EST 2002
Steve Holden wrote:
> If you'd like to find the "last match before a given position" then,
> supposing string p to represent the regex matching the pattern you are
> looking for, you can try match()'ing
>
> ".*(%s)" % p
>
> Given the greedy nature of matching, this should give you the rightmost
> match in groups(1). Maybe. Anyway, an idea to play with.
Hey, that's pretty clever! I wonder about the performance, though...
Anyway, remember that by default .* won't cross newlines.
So: r"(.|\n)*(%s)". Or you could re.DOTALL, but that may change
the meaning of the other regular expression you're plugging in.
## Jason Orendorff http://www.jorendorff.com/
More information about the Python-list
mailing list