Mental Jetsam

By Peter Finch

Unescape a Python “escaped” string

Posted by pcfinch on April 13, 2007

Python has a very useful regular expression function to escape special characters out a string. Oddly, there is no reverse function. Note that python itself will automatically escape the backslash when printing out the string. e.g..

>>> a = re.escape('Special \\#`1\\')
>>> a
'Special\\ \\\\\\#\\`1'\\\\

A simple way to “unescape” the string is to use a regular expression again. The following RE searches the string to a backslash followed by any character and replaced it with that character. The RE selects the character in to a group (.) and then uses that group in the substitution string 1.

>>> z = re.sub(r'\\(.)', r'\1', a)
>>> z
'Special \\#`1\\'

The tick here is the back reference to the character following the escaping “\”. You may think that all you need to do is replace all the “\” characters in the escaped string with nothing “”. Unfortunately, that doesn’t work correctly when you have an escaped “\” e.g. “\\” (which results in an escaped version “\\\\”) … confused?. Performing a simple replace on “\” will result in empty string.

Advertisements

4 Responses to “Unescape a Python “escaped” string”

  1. paftek said

    You might want to consider the following to easily unescape a string :

    >>> ‘\\\\’.decode(‘string_escape’)
    ‘\\’

  2. No re needed said

    Overuse of re is a common and unneeded thing which can slow down calls astronomically, decode will save you a lot of resource expense.

  3. Anonymous said

    Thanks for this tip Paftek.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: