Regexp syntax change in 1.6?

Tim Peters tim_one at email.msn.com
Sun Sep 10 06:42:04 EDT 2000


[Gareth McCaughan]
> ...
> Well, (x?)? should be equivalent to (x)? or (x?), so
> perhaps it's reasonable to be issued a warning. An
> outright error seems rather harsh.

This is a bid for sympathy for poor Fredrik:  while (x)? and (x?) match the
same strings, they're actually *not* the same regexp, thanks to the way
group side-effects are defined.  x? always matches (if "x" isn't there, it
matches a null string), so the group (x?) always has a valid string value
after a successful overall match.  But x needs an "x" to match, so despite
that the overall (x)? will match anything, the *group* (x) may not
participate in a successful match:

>>> re.match("(x?)y", "y").groups()
('',)
>>> re.match("(x)?y", "y").groups()
(None,)
>>>

With all this crap to worry about, a lapse on (x?)? is forgivable <wink>.






More information about the Python-list mailing list