Confusion about re lookahead assertions

Skip Montanaro skip at mojam.com
Sat Apr 1 15:46:34 EST 2000


I'm trying to use re's lookahead assertion constructs for the first time
and am more than a bit confused. If I understand them correctly, the
regular expression r'(?![.])([0-9]+)\s+' should only match digits if they
are not preceeded by a dot, yet the example below clearly contradicts
that.
>>> pat = re.compile(r'(?![.])([0-9]+)\s+')
>>> print pat.search("$5.00 mem")
<re.MatchObject instance at 1ff99a0>
>>> print pat.search("$5.00 mem").group(1)
00
>>> mat = pat.search("$5.00 mem")
>>> print mat
<re.MatchObject instance at 1ff93e0>
>>> print mat.group(1)
00
>>> pat = re.compile(r'(?![-A-Za-z0-9:._])([0-9]+)\s+')
>>> mat = pat.search("$5.00 mem")
>>> print mat
None
I think mat should be None or mat.group(1) should be '0' in both cases.
What am I missing?
--
Skip Montanaro (skip at mojam.com)
Mojam: http://www.mojam.com/
Musi-Cal: http://www.musi-cal.com/


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list