Unmatched Group issue - workaround #43640
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
assignee = 'https://github.com/serhiy-storchaka' closed_at = <Date 2014-10-10.08:45:02.200> created_at = <Date 2006-07-09.18:34:12.000> labels = ['expert-regex', 'type-feature', 'library'] title = 'Unmatched Group issue - workaround' updated_at = <Date 2014-10-10.08:45:02.198> user = 'https://bugs.python.org/nneonneo'
activity = <Date 2014-10-10.08:45:02.198> actor = 'serhiy.storchaka' assignee = 'serhiy.storchaka' closed = True closed_date = <Date 2014-10-10.08:45:02.200> closer = 'serhiy.storchaka' components = ['Library (Lib)', 'Regular Expressions'] creation = <Date 2006-07-09.18:34:12.000> creator = 'nneonneo' dependencies =  files = ['36650'] hgrepos =  issue_num = 1519638 keywords = ['patch'] message_count = 23.0 messages = ['29112', '29113', '29114', '58672', '69541', '69558', '78272', '79830', '79853', '81064', '81118', '81220', '81462', '108662', '108669', '108670', '155967', '155969', '155982', '155983', '227037', '228966', '228969'] nosy_count = 13.0 nosy_names = ['effbot', 'terry.reedy', 'mchaput', 'nneonneo', 'timehorse', 'BMintern', 'ezio.melotti', 'mrabarnett', 'gerardjp', 'THRlWiTi', 'python-dev', 'serhiy.storchaka', 'Nikker'] pr_nums =  priority = 'normal' resolution = 'fixed' stage = 'resolved' status = 'closed' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue1519638' versions = ['Python 3.5']
The text was updated successfully, but these errors were encountered:
Using sre.sub[n], an "unmatched group" error can occur.
The test I used is this pattern:
This will cause the following backtrace to occur:
Traceback (most recent call last): File "<stdin>", line 1, in ? File "lib/python2.4/sre.py", line 142, in sub return _compile(pattern, 0).sub(repl, string, count) File "lib/python2.4/sre.py", line 260, in filter return sre_parse.expand_template(template, match) File "lib/python2.4/sre_parse.py", line 782, in expand_template raise error, "unmatched group" sre_constants.error: unmatched group
Python Version 2.4.3, Mac OS X (behaviour has been verified on
This behaviour, while by design, is unwanted because this type of
The example that I was trying resembles the following:
sre.sub("User: (?:Registered User #(\d+)|Guest)","%USERID|\1%",data)
The intended behaviour is that the function returns "" when the user is
However, when this function encounters a Guest, it raises an exception
Perl and other regex engines behave as I have described, substituting
I found a workaround for the re.sub method so it does not raise an
This is the nutshell:
When doing a search and replace with sub, replace the group represented
If there’s nothing matched by this group the empty subexpression
A complete description is in my post:
Well, in this example the group (ar) is unmatched, so sre throws the
A better example is probably
Can you post the actual text you need this for? The back ref indeed
Symantically speaking ... If there's a "b" then return the "ar", because
I'm having the same issue as the original author of this issue was. The workaround does not apply to the situation where the captured text is on one side of an "or" grouping, rather than just being optional.
I'm trying to remove groups of text in parentheses that come at the end of a string, but if the content in a pair of parentheses is a number, I want to retain it. My regular expression looks like so:
These work: >>> re.sub(r'(?:\((?:(\d+)|.*?)\)\s*)+$','\\1','avatar (2009)') 'avatar 2009' >>> re.sub(r'(?:\((?:(\d+)|.*?)\)\s*)+$','\\1','avatar (2009) (special edition)') 'avatar 2009' This doesn't: >>> re.sub(r'(?:\((?:(\d+)|.*?)\)\s*)+$','\\1','avatar (special Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.6/re.py", line 151, in sub return _compile(pattern, 0).sub(repl, string, count) File "/usr/lib/python2.6/re.py", line 278, in filter return sre_parse.expand_template(template, match) File "/usr/lib/python2.6/sre_parse.py", line 793, in expand_template raise error, "unmatched group" sre_constants.error: unmatched groupedition)')
Is there some way I can apply this workaround to this situation?