Ezio Melotti <ezio.melo...@gmail.com> added the comment:

I'm assuming you want to replace double backslashes with single backslashes in 
stringy_thing, so I defined stringy_thingy and tried both your snippets but 
they are both failing:
>>> stringy_thingy = r'foo\\bar\\baz'
>>> print(stringy_thingy)  # stringy_thingy contains double backslashes
foo\\bar\\baz
>>> re.sub(r'\\\\', chr(92), stringy_thingy)  # fails
Traceback (most recent call last):
  ...
  File "/usr/lib/python3.6/sre_parse.py", line 245, in __next
    self.string, len(self.string) - 1) from None
sre_constants.error: bad escape (end of pattern) at position 0
>>>
>>> parser = re.compile(r'\\\\')
>>> parser.sub(chr(92), stringy_thingy)  # also fails
Traceback (most recent call last):
  ...
  File "/usr/lib/python3.6/sre_parse.py", line 245, in __next
    self.string, len(self.string) - 1) from None
sre_constants.error: bad escape (end of pattern) at position 0

Replacing chr(92) with r'\\' works for both:
>>> print(re.sub(r'\\\\', r'\\', stringy_thingy))
foo\bar\baz
>>> print(parser.sub(r'\\', stringy_thingy))
foo\bar\baz

The docs[0] says: "repl can be a string or a function; if it is a string, any 
backslash escapes in it are processed."
So passing chr(92) (or '\\', which is equivalent) result in the above error 
("bad escape (end of pattern)") because it's seen as an incomplete escape 
sequence.  Passing r'\\' seems to work as intended.

ISTM there is no bug and re.sub works as documented.  Can you provide a 
stringy_thingy for which the first of your snippet fails but the second 
succeeds?

[0]: https://docs.python.org/3/library/re.html#re.sub

----------
resolution:  -> not a bug
stage:  -> test needed
status: open -> pending
type:  -> behavior

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue35072>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to