[Python-bugs-list] [ python-Bugs-576079 ] Inconsistent behaviour in re grouping

noreply@sourceforge.net noreply@sourceforge.net
Mon, 01 Jul 2002 11:36:56 -0700


Bugs item #576079, was opened at 2002-07-01 14:22
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=576079&group_id=5470

Category: Regular Expressions
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Pedro Rodriguez (pedro_rodriguez)
Assigned to: Fredrik Lundh (effbot)
Summary: Inconsistent behaviour in re grouping

Initial Comment:
The following expression (?P<name>.*) and
(?P<name>(.*)) don't behave in the same way. 

When the matching fails, the first group will be None,
but the last one will contain an empty string.

The problem occurs with python 2.1.1 and 2.2. (and
latest CVS for 2.3)

Python 1.5.2 OTH works fine.


(example file attached)

----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2002-07-01 14:36

Message:
Logged In: YES 
user_id=31435

Here's a simpler example:

import re
pat1 = re.compile(r"((.*)x)?(y)")
pat2 = re.compile(r"(((.*))x)?(y)")
print pat1.match('y').groups()
print pat2.match('y').groups()

That prints

(None, None, 'y')
(None, '', None, 'y')

If (y) in the regexps is changed to plain y:

pat1 = re.compile(r"((.*)x)?y")
pat2 = re.compile(r"(((.*))x)?y")
print pat1.match('y').groups()
print pat2.match('y').groups()

the output changes to (the expected):

(None, None)
(None, None, None)

So it's not *just* the extra level of parens -- whether there's 
a capturing group "to the right" also affects the outcome.

FWIW, I agree it's a buglet.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=576079&group_id=5470