You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Strings that end with properly escaped backslashes cause error to be thrown in re.search/sub/etc. functions. #74319
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
GitHub fields:
assignee=Noneclosed_at=<Date2017-11-16.13:25:52.023>created_at=<Date2017-04-21.21:18:05.459>labels= ['expert-regex', '3.7']
title='Strings that end with properly escaped backslashes cause error to be thrown in re.search/sub/etc. functions.'updated_at=<Date2017-11-16.13:25:52.022>user='https://bugs.python.org/PatrickFoley'
If you remove the backslash(es), the code runs fine.
This appears to be specific to the re module and only to strings that end in (even properly escaped) backslashes. You could easily receive raw data like this from freehand input sources so it would be nice not to have to remove trailing backslashes before running a regular expression.
I think you are missing a re.escape around text. Text is otherwise not a valid replacement pattern.
mrabarnett commented on Apr 22, 2017
mrabarnettmannequin
Mannequin
More actions
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:
Good point, re.escape is for literal text you want to insert into a matching pattern, but the replacement template isn't a matching pattern. Do we need a different escape function? I guess the function solution is enough?
mrabarnett commented on Apr 22, 2017
mrabarnettmannequin
Mannequin
More actions
The function solution does have a larger overhead than a literal.
Could the template be made more accepting of backslashes without breaking anything? (There's also bpo-29995 "re.escape() escapes too much", which might help.)
re.escape() shouldn't be used for a replacement template. You need just double backslashes when escape a literal string for a replacement template: s.replace('\\', '\\\\'). This should be documented if still is not documented.
Activity
PatrickFoley commentedon Apr 21, 2017
The following code demonstrates:
If you remove the backslash(es), the code runs fine.
This appears to be specific to the re module and only to strings that end in (even properly escaped) backslashes. You could easily receive raw data like this from freehand input sources so it would be nice not to have to remove trailing backslashes before running a regular expression.
bitdancer commentedon Apr 21, 2017
I think you are missing a re.escape around text. Text is otherwise not a valid replacement pattern.
mrabarnett commentedon Apr 22, 2017
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.escape doesn't always help.
The solution here is to pass a replacement function instead:
bitdancer commentedon Apr 22, 2017
Good point, re.escape is for literal text you want to insert into a matching pattern, but the replacement template isn't a matching pattern. Do we need a different escape function? I guess the function solution is enough?
mrabarnett commentedon Apr 22, 2017
The function solution does have a larger overhead than a literal.
Could the template be made more accepting of backslashes without breaking anything? (There's also bpo-29995 "re.escape() escapes too much", which might help.)
serhiy-storchaka commentedon Apr 22, 2017
re.escape() shouldn't be used for a replacement template. You need just double backslashes when escape a literal string for a replacement template: s.replace('\\', '\\\\'). This should be documented if still is not documented.
serhiy-storchaka commentedon Nov 16, 2017
The proper way of escaping the replacement string has been documented by bpo-31714.