Python 3 Regex and Unicode Emotes
Question: Using Python 3, a simple script like the following should run as intended, but
appears to choke on unicode emote strings:

    
    
    import re
    
    phrase = "(╯°□°)╯ ︵ ┻━┻"
    pattern = r'\b{0}\b'.format(phrase)
    
    text = "The quick brown fox got tired of jumping over dogs and flipped a table: (╯°□°)╯ ︵ ┻━┻"
    
    if re.search(pattern, text, re.IGNORECASE) != None:
        print("Matched!")
    

If I substitute the word "fox" for the contents of the phrase variable, the
pattern does indeed match. I've been puzzled as to why it doesn't like this
particular string though, and my expeditions into the manual and Stack
Overflow haven't illuminated the issue. From all I can tell, Python 3 should
handle this without issue.

Am I missing something painfully obvious?

Edit: Also, dropping the boundaries (\b) doesn't affect the ability to match
the string either.


Answer: 
    (╯°□°)╯ ︵ ┻━┻
    

This expression has brackets in them, you need to escape them. Otherwise they
are interpreted as group.

    
    
    In [24]: re.search(r'\(╯°□°\)╯ ︵ ┻━┻', text, re.IGNORECASE)
    Out[24]: <_sre.SRE_Match object; span=(72, 85), match='(╯°□°)╯ ︵ ┻━┻'>
    
    In [25]: re.findall(r'\(╯°□°\)╯ ︵ ┻━┻', text, re.IGNORECASE)
    Out[25]: ['(╯°□°)╯ ︵ ┻━┻']
    

[Escape the regex string](https://docs.python.org/3/library/re.html#re.escape)
properly and change your code to:

    
    
    import re
    
    phrase = "(╯°□°)╯ ︵ ┻━┻"
    pattern = re.escape(phrase)
    
    text = "The quick brown fox got tired of jumping over dogs and flipped a table: (╯°□°)╯ ︵ ┻━┻"
    
    if re.search(pattern, text, re.IGNORECASE) != None:
        print("Matched!")
    

And then it will work as expected:

    
    
    $ python3 a.py
    Matched!
    

