<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>

[{'answer_id': 13844, 'author': 'Anders Eurenius', 'author_id': 1421, 'author_profile': 'https://Stackoverflow.com/users/1421', 'pm_score': 2, 'selected': False, 'text': '<p>Well, I think you might have missed the r or miscounted the backslashes...</p>\n\n<pre><code>"\\\\n" == r"\\n"\n\n&gt;&gt;&gt; import re\n&gt;&gt;&gt; mystring = r"This is \\\\n a test \\\\r"\n&gt;&gt;&gt; p = re.compile( r"[\\\\][\\\\](.)" )\n&gt;&gt;&gt; print p.sub( r"\\\\\\1", mystring )\nThis is \\n a test \\r\n&gt;&gt;&gt;\n</code></pre>\n\n<p>Which, if I understood is what was requested.</p>\n\n<p>I suspect the more common request is this:</p>\n\n<pre><code>&gt;&gt;&gt; d = {\'n\':\'\\n\', \'r\':\'\\r\', \'f\':\'\\f\'}\n&gt;&gt;&gt; p = re.compile(r"[\\\\]([nrfv])")\n&gt;&gt;&gt; print p.sub(lambda mo: d[mo.group(1)], mystring)\nThis is \\\n a test \\\n&gt;&gt;&gt;\n</code></pre>\n\n<p>The interested student should also read Ken Thompson\'s <a href="http://cm.bell-labs.com/who/ken/trust.html" rel="nofollow noreferrer">Reflections on Trusting Trust"</a>, wherein our hero uses a similar example to explain the perils of trusting compilers you haven\'t bootstrapped from machine code yourself.</p>\n'}, {'answer_id': 13869, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 0, 'selected': False, 'text': '<p>You are being tricked by Python\'s representation of the result string. The Python expression:</p>\n\n<pre><code>\'This is \\\\n a test \\\\r\'\n</code></pre>\n\n<p>represents the string</p>\n\n<pre><code>This is \\n a test \\r\n</code></pre>\n\n<p>which is I think what you wanted. Try adding \'print\' in front of each of your p.sub() calls to print the actual string returned instead of a Python representation of the string.</p>\n\n<pre><code>&gt;&gt;&gt; mystring = r"This is \\n a test \\r"\n&gt;&gt;&gt; mystring\n\'This is \\\\n a test \\\\r\'\n&gt;&gt;&gt; print mystring\nThis is \\n a test \\r\n</code></pre>\n'}, {'answer_id': 13882, 'author': 'eplawless', 'author_id': 1370, 'author_profile': 'https://Stackoverflow.com/users/1370', 'pm_score': 1, 'selected': False, 'text': '<p>The idea is that I\'ll read in an escaped string, and unescape it (a feature notably lacking from Python, which you shouldn\'t need to resort to regular expressions for in the first place). Unfortunately I\'m not being tricked by the backslashes...</p>\n\n<p>Another illustrative example:</p>\n\n<pre><code>&gt;&gt;&gt; mystring = r"This is \\n ridiculous"\n&gt;&gt;&gt; print mystring\nThis is \\n ridiculous\n&gt;&gt;&gt; p = re.compile( r"\\\\(\\S)" )\n&gt;&gt;&gt; print p.sub( \'bloody\', mystring )\nThis is bloody ridiculous\n&gt;&gt;&gt; print p.sub( r\'\\1\', mystring )\nThis is n ridiculous\n&gt;&gt;&gt; print p.sub( r\'\\\\1\', mystring )\nThis is \\1 ridiculous\n&gt;&gt;&gt; print p.sub( r\'\\\\\\1\', mystring )\nThis is \\n ridiculous\n</code></pre>\n\n<p>What I\'d like it to print is</p>\n\n<pre><code>This is \nridiculous\n</code></pre>\n'}, {'answer_id': 13943, 'author': 'markpasc', 'author_id': 1472, 'author_profile': 'https://Stackoverflow.com/users/1472', 'pm_score': 4, 'selected': True, 'text': '<p>Isn\'t that what Anders\' second example does?</p>\n\n<p>In 2.5 there\'s also a <code>string-escape</code> encoding you can apply:</p>\n\n<pre><code>&gt;&gt;&gt; mystring = r"This is \\n a test \\r"\n&gt;&gt;&gt; mystring.decode(\'string-escape\')\n\'This is \\n a test \\r\'\n&gt;&gt;&gt; print mystring.decode(\'string-escape\')\nThis is \n a test \n&gt;&gt;&gt; \n</code></pre>\n'}, {'answer_id': 13958, 'author': 'eplawless', 'author_id': 1370, 'author_profile': 'https://Stackoverflow.com/users/1370', 'pm_score': 0, 'selected': False, 'text': "<p>Mark; his second example requires every escaped character thrown into an array initially, which generates a KeyError if the escape sequence happens not to be in the array. It will die on anything but the three characters provided (give \\v a try), and enumerating every possible escape sequence every time you want to unescape a string (or keeping a global array) is a really bad solution. Analogous to PHP, that's using <code>preg_replace_callback()</code> with a lambda instead of <code>preg_replace()</code>, which is utterly unnecessary in this situation.</p>\n\n<p>I'm sorry if I'm coming off as a dick about it, I'm just utterly frustrated with Python. This is supported by every other regular expression engine I've ever used, and I can't understand why this wouldn't work.</p>\n\n<p>Thank you for responding; the <code>string.decode('string-escape')</code> function is precisely what i was looking for initially. If someone has a general solution to the regex backreference problem, feel free to post it and I'll accept that as an answer as well.</p>\n"}]
['https://Stackoverflow.com/questions/13791', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1370/']