Search code examples
pythonregexescapingspecial-characters

How can Python's regular expressions work with patterns that have escaped special characters?


Is there a way to get Python's regular expressions to work with patterns that have escaped special characters? As far as my limited understanding can tell, the following example should work, but the pattern fails to match.

import re

string = r'This a string with ^g\.$s'   # A string to search
pattern = r'^g\.$s'                     # The pattern to use

string = re.escape(string)              # Escape special characters
pattern = re.escape(pattern)

print(re.search(pattern, string))       # This prints "None"

Note:
Yes, this question has been asked elsewhere (like here). But as you can see, I'm already implementing the solution described in the answers and it's still not working.


Solution

  • Why on earth are you applying re.escape to the string?! You want to find the "special" characters in that! If you just apply it to the pattern, you'll get a match:

    >>> import re
    >>> string = r'This a string with ^g\.$s'
    >>> pattern = r'^g\.$s'
    >>> re.search(re.escape(pattern), re.escape(string))  # nope
    >>> re.search(re.escape(pattern), string)  # yep
    <_sre.SRE_Match object at 0x025089F8>
    

    For bonus points, notice that you just need to re.escape the pattern one more times than the string:

    >>> re.search(re.escape(re.escape(pattern)), re.escape(string))
    <_sre.SRE_Match object at 0x025D8DE8>