Does Python have a function that I can use to escape special characters in a regular expression?
I'm "stuck" :\ should become
I\'m \"stuck\" :\\.
>>> import re >>> re.escape(r'\ a.*$') '\\\\\\ a\\.\\*\\$' >>> print(re.escape(r'\ a.*$')) \\\ a\.\*\$ >>> re.escape('www.stackoverflow.com') 'www\\.stackoverflow\\.com' >>> print(re.escape('www.stackoverflow.com')) www\.stackoverflow\.com
Repeating it here:
Return string with all non-alphanumerics backslashed; this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.
I'm surprised no one has mentioned using regular expressions via
import re print re.sub(r'([\"])', r'\\\1', 'it\'s "this"') # it's \"this\" print re.sub(r"([\'])", r'\\\1', 'it\'s "this"') # it\'s "this" print re.sub(r'([\" \'])', r'\\\1', 'it\'s "this"') # it\'s\ \"this\"
Important things to note:
\as well as the character(s) you're looking for. You're going to be using
\to escape your characters, so you need to escape that as well.
([\"]), so that the substitution pattern can use the found character when it adds
\in front of it. (That's what
\1does: uses the value of the first parenthesized group.)
rin front of
r'([\"])'means it's a raw string. Raw strings use different rules for escaping backslashes. To write
([\"])as a plain string, you'd need to double all the backslashes and write
'([\\"])'. Raw strings are friendlier when you're writing regular expressions.
\to distinguish it from a backslash that precedes a substitution group, e.g.
r'\\\1'. To write that as a plain string, you'd need
'\\\\\\1'— and nobody wants that.