Serhiy Storchaka added the comment:
chr(92) is a single backspace character ('\\'). Since backspace has special
meaning in the replacement string, it should be escaped.
re.sub(r'', r'\\', stringy_thingy)
--
nosy: +serhiy.storchaka
stage: test needed -> resolved
status: open -
Matthew Barnett added the comment:
@Ezio: the value of stringy_thingy is irrelevant because it never gets that
far; it fails when it tries to parse the replacement, which occurs before
attempting any matching.
I can't reproduce the difference either.
--
status: pending -> open
Ezio Melotti 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 c
New submission from Samuel Warfield :
Bug with regex substitutions. When calling the re.sub() method directly
char(92), the double backslash charecter as the replacement, throws an
exception. Whereas compiling a regex object then calling its own .sub()
method works completely fine. I did a quick