[Python-Dev] Behavior of matching backreferences

Gustavo Niemeyer niemeyer@conectiva.com
Sun, 23 Jun 2002 01:39:36 -0300


> I think your re has a bug in it that in python would be
> 
>     if cond:
>         a = 1
>     print a
> 
> python will give an error is cond is false.
> 
> An re that defines a group conditionally as yours does I think
> is the same programming error. That's the ambiguity I am
> referring to, is or is not the named group defined?

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'"
>>>

[...]
> You can prevent groups being remember using the (?:...) syntax
> if you need to preserve the group index. So you need:
> 
>     "^(?P<a>(?:abc)?)(?P=a)"

Again, you may do regular expressions in many ways, the point I'm
still raising is that there's one way that doesn't work as expected.

> I'm not convinced you have found a bug in the engine that needs
> fixing, I think its your re needs changing. I want the re engine
> to report the error for re that are illogical.

The re won't report anything when somebody uses this syntax. It will
just don't work as expected. If you think this re is illogical, don't
use it. But I see no point in denying others to use it.

I'm not planning to discuss much more about this. My intentions and the
issue are clear enough. I'd like to hear the opinion of Fredrik about
this, though.

-- 
Gustavo Niemeyer

[ 2AAC 7928 0FBF 0299 5EB5  60E2 2253 B29A 6664 3A0C ]