[issue30133] Strings that end with properly escaped backslashes cause error to be thrown in re.search/sub/etc. functions.

2017-11-16 Thread Serhiy Storchaka

Serhiy Storchaka  added the comment:

The proper way of escaping the replacement string has been documented by 
issue31714.

--
resolution:  -> out of date
stage:  -> resolved
status: open -> closed
superseder:  -> Improve re documentation

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30133] Strings that end with properly escaped backslashes cause error to be thrown in re.search/sub/etc. functions.

2017-04-28 Thread Terry J. Reedy

Changes by Terry J. Reedy :


--
versions: +Python 3.6, Python 3.7 -Python 3.4

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30133] Strings that end with properly escaped backslashes cause error to be thrown in re.search/sub/etc. functions.

2017-04-21 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

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.

--
nosy: +serhiy.storchaka

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30133] Strings that end with properly escaped backslashes cause error to be thrown in re.search/sub/etc. functions.

2017-04-21 Thread Matthew Barnett

Matthew Barnett added the comment:

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 issue29995 "re.escape() escapes too much", which might 
help.)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30133] Strings that end with properly escaped backslashes cause error to be thrown in re.search/sub/etc. functions.

2017-04-21 Thread R. David Murray

R. David Murray added the comment:

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?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30133] Strings that end with properly escaped backslashes cause error to be thrown in re.search/sub/etc. functions.

2017-04-21 Thread Matthew Barnett

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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30133] Strings that end with properly escaped backslashes cause error to be thrown in re.search/sub/etc. functions.

2017-04-21 Thread R. David Murray

R. David Murray added the comment:

I think you are missing a re.escape around text.  Text is otherwise not a valid 
replacement pattern.

--
nosy: +r.david.murray

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30133] Strings that end with properly escaped backslashes cause error to be thrown in re.search/sub/etc. functions.

2017-04-21 Thread Patrick Foley

New submission from Patrick Foley:

The following code demonstrates:

import re
text = 'ab\\'
exp = re.compile('a')
print(re.sub(exp, text, ''))

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.

--
components: Regular Expressions
files: sample.py
messages: 292079
nosy: Patrick Foley, ezio.melotti, mrabarnett
priority: normal
severity: normal
status: open
title: Strings that end with properly escaped backslashes cause error to be 
thrown in re.search/sub/etc. functions.
versions: Python 2.7, Python 3.4
Added file: http://bugs.python.org/file46822/sample.py

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com