4

Example code:

#!/usr/bin/env python
import re

print re.sub(r'a+(\d)?', r'\1', "aaaa3")
print re.sub(r'a+(\d)?', r'\1', "aaaa") # error!

The second print statement gives me an error:

3
Traceback (most recent call last):
  File "./bbb.py", line 5, in <module>
    print re.sub(r'a+(\d)?', r'\1', "aaaa")
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 155, in sub
    return _compile(pattern, flags).sub(repl, string, count)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 291, in filter
    return sre_parse.expand_template(template, match)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_parse.py", line 831, in expand_template
    raise error, "unmatched group"
sre_constants.error: unmatched group

How can I deal with this capturing variable with a probable quantifier 0 without an error?

Note (\d)? here can be another complicated regex, not just an easy as \d, that why I bring my quantifier ? out out (..).

fronthem
  • 4,011
  • 8
  • 34
  • 55

1 Answers1

2

In Python you can do this to get an empty back-reference of an optional group:

>>> print re.sub(r'a+(\d?)', r'\1', "aaaa")

>>> print re.sub(r'a+(\d?)', r'\1', "aaaa123")
123

i.e. use (\d?) instead of (\d)?

Python regex engine unlike many other regex engines doesn't populate back-references when corresponding capturing group fails to match a pattern.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
anubhava
  • 761,203
  • 64
  • 569
  • 643