[issue30133] Strings that end with properly escaped backslashes cause error to be thrown in re.search/sub/etc. functions.
Matthew Barnett
report at bugs.python.org
Fri Apr 21 21:17:58 EDT 2017
Matthew Barnett added the comment:
Yes, the second argument is a replacement template, not a literal.
This issue does point out a different problem, though: re.escape will add backslashes that will then be treated as literals in the template, for example:
>>> re.sub(r'a', re.escape('(A)'), 'a')
'\\(A\\)'
re.escape doesn't always help.
The solution here is to pass a replacement function instead:
>>> re.sub(r'a', lambda m: '(A)', 'a')
'(A)'
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30133>
_______________________________________
More information about the Python-bugs-list
mailing list