<p>I am trying to implement string unescaping with Python regex and backreferences, and it doesn't seem to want to work very well. I'm sure it's something I'm doing wrong but I can't figure out what...</p>

<pre><code>&gt;&gt;&gt; import re
&gt;&gt;&gt; mystring = r"This is \n a test \r"
&gt;&gt;&gt; p = re.compile( "\\\\(\\S)" )
&gt;&gt;&gt; p.sub( "\\1", mystring )
'This is n a test r'
&gt;&gt;&gt; p.sub( "\\\\\\1", mystring )
'This is \\n a test \\r'
&gt;&gt;&gt; p.sub( "\\\\1", mystring )
'This is \\1 a test \\1'
</code></pre>

<p>I'd like to replace \\[char] with \[char], but backreferences in Python don't appear to follow the same rules they do in every other implementation I've ever used. Could someone shed some light?</p>

Well, I think you might have missed the r or miscounted the backslashes...
```
```
"\\n" == r"\n"

>>> import re
>>> mystring = r"This is \\n a test \\r"
>>> p = re.compile( r"[\\][\\](.)" )
>>> print p.sub( r"\\\1", mystring )
This is \n a test \r
>>>
```
```
Which, if I understood is what was requested.
I suspect the more common request is this:
```
```
>>> d = {'n':'\n', 'r':'\r', 'f':'\f'}
>>> p = re.compile(r"[\\]([nrfv])")
>>> print p.sub(lambda mo: d[mo.group(1)], mystring)
This is \
 a test \
>>>
```
```
The interested student should also read Ken Thompson's
Reflections on Trusting Trust"
, wherein our hero uses a similar example to explain the perils of trusting compilers you haven't bootstrapped from machine code yourself.