[Python-bugs-list] [ python-Bugs-645843 ] findall returns '' instead of None

noreply@sourceforge.net noreply@sourceforge.net
Sun, 01 Dec 2002 03:20:09 -0800


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

Category: Regular Expressions
Group: Python 2.2.2
Status: Closed
Resolution: Wont Fix
Priority: 5
Submitted By: Zooko O'Whielacronx (zooko)
Assigned to: Fredrik Lundh (effbot)
Summary: findall returns '' instead of None

Initial Comment:
Dear Pythonistas:

Thank you for Python!

>>> import re
>>> re.match("(B+)(A+)?", "B").groups() # test 1
('B', None)
>>> re.match("(B+)(A*)", "B").groups() # test 2
('B', '')
>>> re.findall("(B+)(A+)?", "B") # test 3
[('B', '')]
>>> re.findall("(B+)(A*)", "B") # test 4
[('B', '')]

I was expecting test 3 to return [('B', None)], so this
was a surprise to me.  I also think that this behavior
would be better, so that the results from test 3 and
test 4 can be distinguished.  (It doesn't matter in
this test, but it could if there is a '|' disjunction,
and I want to use "is None" to determine which side
matched.)

If you don't want to change the semantics, perhaps for
compatibility reasons, or because you prefer these
ones, then I suggest updating the doc of "findall" to
mention this behavior.

Regards,

Zooko


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

>Comment By: Fredrik Lundh (effbot)
Date: 2002-12-01 12:20

Message:
Logged In: YES 
user_id=38376

(footnote: to get a real match object, use finditer)

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

Comment By: Fredrik Lundh (effbot)
Date: 2002-12-01 10:09

Message:
Logged In: YES 
user_id=38376

"pre" (Python 1.5.2's regular expression engine) is the
reference for all semantic issues, and it does the same
thing.

Fred, can you add a brief note to the findall documentation
(when findall returns tuples, it uses "" for both empty and
non-existent matches)

Zooko, if you really want the code to be changed, I suggest
raising the issue on python-dev; we can always turn this bug
report into a feature request later on.

</F>

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

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-11-30 22:22

Message:
Logged In: YES 
user_id=80475

Yes, test #3 looks like a bug to me.
(A+) should not even be an empty match.

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

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