2 Answers
2

Return the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in string by the
replacement repl. repl can be either a string or a callable;
if a string, backslash escapes in it are processed. If it is
a callable, it's passed the match object and must return
a replacement string to be used.

One way to get around this is to pass a lambda:

>>> reg.sub(rpl, my_string )
'I hope this This is a nicely escaped newline \n is replaced with a nicely escaped string'
>>> reg.sub(lambda x: rpl, my_string )
'I hope this This is a nicely escaped newline \\n is replaced with a nicely escaped string'

Weird, wonder why it does that. Thanks for explaining! I've ended up doing rpl.encode('escape_string'), as it makes the code very understandable
–
mowwwalkerAug 26 '12 at 4:14

@Walkerneo: Replacement patterns are unescaped, but callables are expected to return the exact string that they want to replace (as it's implied they would do any necessary processing already). Hence, the output from a callable replacement is not unescaped.
–
nneonneoAug 26 '12 at 4:20

1

@nneonneo, Thank you, I understand that, but it does make the code look a bit more cryptic. Someone reading it probably won't see the use in using a lambda expression that just returns a string.
–
mowwwalkerAug 26 '12 at 4:25

@Walkerneo: if only there were a way to leave a short message in the code for the reader which would explain it.. :^) More seriously, string_escape (not escape_string) seems like a perfectly viable approach.
–
DSMAug 26 '12 at 4:32

2

The reason backslashes are escaped is that the replacement isn't just a plain string but a regex replacement pattern. It can, for instance, contain backreferences like \1 to include groups from the match. Since at least some escapes have to be processed, it makes sense to process them all.
–
BrenBarnAug 26 '12 at 4:41

All regex patterns used for Python's re module are unescaped, including both search and replacement patterns. This is why the r modifier is generally used with regex patterns in Python, as it reduces the amount of "backwhacking" necessary to write usable patterns.