Regexes: How to handle escaped characters

Torsten Bronger bronger at physik.rwth-aachen.de
Fri May 18 14:09:19 EDT 2007


Hallöchen!

Charles Sanders writes:

> Torsten Bronger wrote:
>
> [...]
>
>>>> Example string: u"Hollo", escaped positions: [4].  Thus, the
>>>> second "o" is escaped and must not be found be the regexp
>>>> searches.
>>>>
>>>> Instead of re.search, I call the function guarded_search(pattern,
>>>> text, offset) which takes care of escaped caracters.  Thus, while
>
> 	I'm still pretty much a beginner, and I am not sure
> of the exact requirements, but the following seems to work
> for at least simple cases when overlapping matches are not
> considered.
>
> def guarded_search( pattern, text, exclude ):
>   return [ m for m in re.finditer(pattern,text)
>     if not [ e for e in exclude if m.start() <= e < m.end() ] ]

Yes, this seems to do the trick, thank you!

Tschö,
Torsten.

-- 
Torsten Bronger, aquisgrana, europa vetus
                                      Jabber ID: bronger at jabber.org
                      (See http://ime.webhop.org for ICQ, MSN, etc.)



More information about the Python-list mailing list