[issue4185] re module treats raw strings as normal strings

Ezio Melotti report at bugs.python.org
Thu Oct 23 06:30:59 CEST 2008


Ezio Melotti <ezio.melotti at gmail.com> added the comment:

My bad, I only checked with help(re.sub).
In the examples with re.search I was indeed wrong because I forgot to
escape the \ and for the regex engine \n is the same of n (whereas \\n
is a literal \ followed by n), but I expected 'a1a1a'.replace('1',
r'\n') to return the same of re.sub('1', r'\n', 'a1a1a') because the
r'\n' is not a regex but a simple replacement string.
Also, the doc says "repl can be a string or a function; if it is a
string, any backslash escapes in it are processed. That is, \n is
converted to a single newline character, \r is converted to a linefeed,
and so forth.", this is the standard behavior of normal string, it
should be mentioned that the backslashes are processed even if with raw
strings and they need to be escaped with two \.
I think that changing the behavior of what is supposed to be a "normal
string" (the repl string) is not really a good idea (even if it's useful
when you have things like '\1\n' and assuming that this is why it has
different behavior), I'd rather prefer to use $1 instead of \1. Unlike
'\1', (as far as I know) $1 has no special meaning in Python so there
won't be any problem with raw strings.

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4185>
_______________________________________


More information about the Python-bugs-list mailing list