searching backwards in a string

Paul Rubin phr-n2002a at nightsong.com
Tue Feb 12 18:35:06 EST 2002


"Mark McEahern" <marklists at mceahern.com> writes:
> That's probably true.  Would you be so kind as to post an example of the
> input (string, complicated regexp) and output (do you want it to tell you
> the starting position from left to right of the first match found backwards
> in the string?) you'd expect?

Input string: contents of some random html file

Pattern: match the beginning of an html open or close tag:

  pattern = re.compile("<(/?)(.*?)\b")

How do you search backwards in the html file, starting from location 50000?

I propose adding a "direction flag" to the search function:

   match = pattern.search(text, 50000, -1, direction=-1)

here 50000 and -1 are the starting and ending locations.



More information about the Python-list mailing list