Regexes: How to handle escaped characters

John Machin sjmachin at lexicon.net
Thu May 17 17:17:23 EDT 2007


On May 18, 6:50 am, James Stroud <jstr... at mbi.ucla.edu> wrote:

> def guarded_search(rgx, astring, escaped):
>    m = re.search(rgx, astring)
>    if m:
>      s = m.start()
>      e = m.end()
>      for i in escaped:
>        if s <= i <= e:

Did you mean to write

if s <= i < e:

?


>          m = None
>          break
>    return m
>

Your guarded search fails if there is a match after the rightmost bad
position i.e. it gives up at the first bad position.

My "guarded_search" (see separated post) needs the following done to
it:
1. make a copy
2. change name of copy to "guarded_searchall" or something similar
3. change "yield" to "return" in the original

Cheers,
John




More information about the Python-list mailing list