[Python-Dev] Behavior of matching backreferences

Barry Scott barry@barrys-emacs.org
Sun, 23 Jun 2002 00:40:25 +0100


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?

> If you think about a match with more characters, you'll end up in
> something like "^(?P<a>(abc)?)(?P=a)", instead of "^(?P<a>abc)?(?P=a)".
> Besides having a little difference in their meanings (the first
> m.group(1) is '', and the second is None), it looks like you're
> workarounding an existant problem, but you may argue that this opinion
> is something personal.

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

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.

	BArry