问题
I have the next regular expression to find emojis on a text:
re.compile(u'([\U00002600-\U000027BF])|([\U0001F300-\U0001F64F])|([\U0001F680-\U0001F6FF])')
It is working well in Python 3 but in Python 2.7 I get this:
sre_constants.error: bad character range
How can I fix it to support both, Python 2.7 and Python 3?
回答1:
Use r'(... instead of u'(... like this:
re.compile(r'([\U00002600-\U000027BF\U0001F300-\U0001F64F\U0001F680-\U0001F6FF])')
Also note that you can specify multiple ranges inside [...]
https://regex101.com/r/WuQ3Zr/1
来源:https://stackoverflow.com/questions/44314290/python-regex-bad-character-range