Confusion about re lookahead assertions

Darrell darrell at dorb.com
Sat Apr 1 23:20:17 CEST 2000


Looks like a negative lookahead assertion doesn't consume anything.
The doc for ?= says this but ?! doesn't.
So the (?![.]) matches the "0". I didn't know that you could use "." instead
of "\."

>From doc:
(?=...)
Matches if ... matches next, but doesn't consume any of the string

--Darrell

"Skip Montanaro"
> 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?






More information about the Python-list mailing list