[New-bugs-announce] [issue25953] re fails to identify invalid numeric group references in replacement strings

bazwal report at bugs.python.org
Fri Dec 25 14:46:48 EST 2015


New submission from bazwal:

This code example:

    re.sub(r'(?P<x>[123])', r'\g<a>', '')

will correctly raise a KeyError due to the invalid group reference.

However, this very similar code example:

    re.sub(r'(?P<x>[123])', r'\g<3>', '')

fails to raise an error. It seems that the only way to check whether a numeric group reference is compatible with a given pattern is to test it against a string which happens to match. But this is obviously infeasible when checking unknown expressions (e.g. those taken from user input). And in any case: errors should be raised at the point where they occur (i.e. during compilation), not at some indeterminate point in the future.

Regular expression objects have a "groups" attribute which holds the number of capturing groups in the pattern. So there seems no good reason why the replacement string parser can't identify invalid numeric group references in exactly the same way that it does for symbolic ones.

----------
components: Regular Expressions
messages: 257008
nosy: bazwal, ezio.melotti, mrabarnett
priority: normal
severity: normal
status: open
title: re fails to identify invalid numeric group references in replacement strings
type: behavior
versions: Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25953>
_______________________________________


More information about the New-bugs-announce mailing list