Regular expressions help

Robert Brewer fumanchu at amor.org
Tue Feb 24 14:34:07 EST 2004


Mike Hearne wrote:
> Given the following three lines:
> 
> name fredgeorge
> name georgeharry
> name george
> 
> I'm trying to find a regular expression that matches only the third
> one.

>>> import re
>>> t = 'name fredgeorge\nname georgeharry\nname george'
>>> pattern = '(?<!\w)george(?!\w)'
>>> m = re.search(pattern, t)
>>> m.span()
(38, 44)

HTH. Basic interpretation is we search for 'george' that is neither
preceded by, nor followed by, a word character. Not that this allows
'george' to be followed by EOF.

FuManChu




More information about the Python-list mailing list