[ python-Bugs-1113484 ] document {m} regex matcher wrt empty matches

SourceForge.net noreply at sourceforge.net
Wed Sep 14 11:17:38 CEST 2005


Bugs item #1113484, was opened at 2005-01-31 21:46
Message generated for change (Comment added) made by niemeyer
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1113484&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Regular Expressions
Group: None
>Status: Closed
>Resolution: Works For Me
Priority: 5
Submitted By: Wummel (calvin)
Assigned to: Gustavo Niemeyer (niemeyer)
Summary: document {m} regex matcher wrt empty matches

Initial Comment:
The {m} matcher seems not to be applicable to (some)
empty matches. For example this will raise a regex
compile error:
>>> re.compile("(a*){4}")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.3/sre.py", line 179, in compile
    return _compile(pattern, flags)
  File "/usr/lib/python2.3/sre.py", line 230, in _compile
    raise error, v # invalid expression
sre_constants.error: nothing to repeat

However this matcher is compiled without error:
>>> re.compile("(\ba*){4}")
<_sre.SRE_Pattern object at 0xb7f86c58>

I don't know why the first example gives an error, but
it should perhaps be mentioned in the documentation
about the {} regex operator.

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

>Comment By: Gustavo Niemeyer (niemeyer)
Date: 2005-09-14 09:17

Message:
Logged In: YES 
user_id=7887

Would you be able to come up with an example that would be
useful for that kind of construction?

"(a*){4}" will always match "a" as many times as possible,
and than match the empty string 3 more times. So it has the
effect of "a*", but in addition will kill the grouping
effect since the given group will always be empty. With that
in mind considering it as a syntax error seems correct.

Do you agree?


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

Comment By: Wummel (calvin)
Date: 2005-02-03 17:06

Message:
Logged In: YES 
user_id=9205

Oops, it should have been:
>>> re.compile(r"(\ba*){4}")
And now the error is consistent (now tested in Python 2.4
instead of 2.3):
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.4/sre.py", line 180, in compile
    return _compile(pattern, flags)
  File "/usr/lib/python2.4/sre.py", line 227, in _compile
    raise error, v # invalid expression
sre_constants.error: nothing to repeat

So it seems that {m} operator does not like potentially
empty matches.

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

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


More information about the Python-bugs-list mailing list