The Python Oracle

Reversing Python's re.escape

This video explains
Reversing Python's re.escape

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Techno Intrigue Looping

--

Chapters
00:00 Question
00:58 Accepted answer (Score 10)
01:59 Thank you

--

Full question
https://stackoverflow.com/questions/4366...

Question links:
[This blog from 2007]: https://mentaljetsam.wordpress.com/2007/.../
[Some suggest]: https://stackoverflow.com/questions/1482...

Accepted answer links:
[source]: https://github.com/python/cpython/blob/3...
[re.escape()]: https://github.com/python/cpython/blob/3...
[str.translate()]: https://docs.python.org/3/library/stdtyp...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #regex #python3x #escaping

#avk47



ACCEPTED ANSWER

Score 10


So is this really the only thing that works?

>>> re.sub(r'\\(.)', r'\1', re.escape(' '))
' '

Yes. The source for the re module contains no unescape() function, so you're definitely going to have to write one yourself.

Furthermore, the re.escape() function uses str.translate()

def escape(pattern):
    """
    Escape special characters in a string.
    """
    if isinstance(pattern, str):
        return pattern.translate(_special_chars_map)
    else:
        pattern = str(pattern, 'latin1')
        return pattern.translate(_special_chars_map).encode('latin1')

… which, while it can transform a single character into multiple characters (e.g. [\[), cannot perform the reverse of that operation.

Since there's no direct reversal of escape() available via str.translate(), a custom unescape() function using re.sub(), as described in your question, is the most straightforward solution.