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 <[email protected]>
<http://bugs.python.org/issue25953>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com