Author bazwal
Recipients bazwal, ezio.melotti, mrabarnett
Date 2015-12-25.19:46:47
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1451072808.1.0.287523380704.issue25953@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2015-12-25 19:46:48bazwalsetrecipients: + bazwal, ezio.melotti, mrabarnett
2015-12-25 19:46:48bazwalsetmessageid: <1451072808.1.0.287523380704.issue25953@psf.upfronthosting.co.za>
2015-12-25 19:46:48bazwallinkissue25953 messages
2015-12-25 19:46:47bazwalcreate