searching backwards in a string

Steve Holden sholden at holdenweb.com
Tue Feb 12 10:17:08 EST 2002


"Paul Rubin" <phr-n2002a at nightsong.com> wrote in message
news:7xk7tjayos.fsf at ruckus.brouhaha.com...
> I have a string and want to search backwards for a regexp, starting at
> a given index, like if you search backwards in a text editor.  Is
> there a way to do that with re?  The docs aren't clear about what's
> supposed to happen if the ending position to the pat.search method is
> less than the starting position, but I tried it and it doesn't seem
> to do what I want.  Any advice appreciated--thanks.

Warning: IANOREG

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.

regards
 Steve
--
Consulting, training, speaking: http://www.holdenweb.com/
Author, Python Web Programming: http://pydish.holdenweb.com/pwp/

"This is Python.  We don't care much about theory, except where it
intersects with useful practice."  Aahz Maruch on c.l.py







More information about the Python-list mailing list