Replacing "\"s using re.sub
Andrew M. Kuchling
akuchlin at mems-exchange.org
Sat Feb 19 13:14:04 EST 2000
"Edward C. Jones" <edcjones at erols.com> writes:
> I need to replace all instances of "\" in a string with "\\". The
> function "re.sub" looks like the way to do it. "re.sub" is called
> by "re.sub(pattern, repl, string)". For my problem what should
> "pattern" and "repl" be?
If you're only dealing with a substitution of one exact string with
another exact string, it's faster and easier to use string.replace:
S2 = string.replace(S, '\\', '\\\\')
If you must use regular expressions for some reason (sigh), think
about the problem a bit. "pattern" needs to match a single \ ; \ is
used as a special character in regular expressions, so to match a
single \ the pattern has to be \\ .
You want the replacement to be \\, but \ is also special in
replacement strings for things like \g<2>, so you need to double them
both.
The patterns need to be inside strings, and Python also uses \ as a
special character; use a raw string, as in r"contents", to prevent
Python from paying attention to \..
S = "String with \\ in it; here's another \\ "
S2 = re.sub(r'\\', r'\\\\', S)
print S
print S2
This outputs:
String with \ in it; here's another \
String with \\ in it; here's another \\
If you didn't use a raw string, you'd need yet another level of \ quoting,
doubling all the backslashes again:
S2 = re.sub('\\\\', '\\\\\\\\', S)
--
A.M. Kuchling http://starship.python.net/crew/amk/
Sending a newgroup message without permission of Leader Kibo: Poster is forced
to adopt twelve wacky sitcom children.
-- Kibo, in the Happynet Manifesto
More information about the Python-list
mailing list