[Python-Dev] Behavior of matching backreferences

Roman Suzi rnd@onego.ru
Sun, 23 Jun 2002 20:10:43 +0400 (MSD)


On Sun, 23 Jun 2002, Gustavo Niemeyer wrote:

I do not agree with both of you. I think, re should give an error at compile
time (as it does in cases, like (?<=REGEXP), where only fixed length is
allowed:

>>> re.compile("(?<=R*)")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.2/sre.py", line 178, in compile
    return _compile(pattern, flags)
  File "/usr/lib/python2.2/sre.py", line 228, in _compile
    raise error, v # invalid expression
sre_constants.error: look-behind requires fixed-width pattern


Why? Because there is no sense in matching non-existent group.
It's simply incorrect. So, instead of having time-bombs
Gustavo found, it's better to check at re compile time.

>Sorry Barry, but I don't see your point here. There's no change in
>the naming semantics. In sre that's totally valid and used in a
>lot of code:
>
>>>> `re.compile("(?P<a>a)?").match("b").group("a")`
>'None'
>>>> `re.compile("(?P<a>a)?").match("a").group("a")`
>"'a'"
>>>>

This is quite different. None has a sense of meta-value
which indicates that group was not used while matching.
There is no way to use it in the re consistently.

(well, probably some syntax could be invented for it,
like 'match only if exists', etc. But it is too subtle
and is hardly needed).

Sincerely yours, Roman Suzi
-- 
rnd@onego.ru =\= My AI powered by Linux RedHat 7.2