Python regex: Bad character range

二次信任 提交于 2021-01-28 07:50:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!