inserting \ in regular expressions

Ross Boylan ross at biostat.ucsf.edu
Wed Oct 26 15:48:01 EDT 2011


I want to replace every \ and " (the two characters for backslash and
double quotes) with a \ and the same character, i.e.,
\ -> \\
" -> \"

I have not been able to figure out how to do that.  The documentation
for re.sub 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 carriage return, and so
forth. Unknown escapes such as \j are left alone."

\\ is apparently unknown, and so is left as is. So I'm unable to get a
single \.

Here are some tries in Python 2.5.2.  The document suggested the result
of a function might not be subject to the same problem, but it seems to
be.
>>> def f(m):
...    return "\\"+m.group(1)
... 
>>> re.sub(r"([\\\"])", f, 'Silly " quote')
'Silly \\" quote'
>>> re.sub(r"([\\\"])", r"\\1", 'Silly " quote')
'Silly \\1 quote'
>>> re.sub(r"([\\\"])", "\\\\1", 'Silly " quote')
'Silly \\1 quote'
>>> re.sub(r"([\\\"])", "\\\\\1", 'Silly " quote')
'Silly \\\x01 quote'
>>> re.sub(r"([\\\"])", "\\\\\\1", 'Silly " quote')
'Silly \\" quote'

Or perhaps I'm confused about what the displayed results mean.  If a
string has a literal \, does it get shown as \\?

I'd appreciate it if you cc me on the reply.

Thanks.
Ross Boylan




More information about the Python-list mailing list