This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: document {m} regex matcher wrt empty matches
Type: Stage:
Components: Regular Expressions Versions:
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: niemeyer Nosy List: calvin, niemeyer
Priority: normal Keywords:

Created on 2005-01-31 21:46 by calvin, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg24118 - (view) Author: Bastian Kleineidam (calvin) Date: 2005-01-31 21:46
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.
msg24119 - (view) Author: Bastian Kleineidam (calvin) Date: 2005-02-03 17:06
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.
msg24120 - (view) Author: Gustavo Niemeyer (niemeyer) * (Python committer) Date: 2005-09-14 09:17
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?
History
Date User Action Args
2022-04-11 14:56:09adminsetgithub: 41511
2005-01-31 21:46:48calvincreate