Python 正则表达式:错误的字符范围 [英] Python regex: Bad character range

查看:44
本文介绍了Python 正则表达式:错误的字符范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一个正则表达式来查找文本上的表情符号:

I have the next regular expression to find emojis on a text:

re.compile(u'([\U00002600-\U000027BF])|([\U0001F300-\U0001F64F])|([\U0001F680-\U0001F6FF])')

它在 Python 3 中运行良好,但在 Python 2.7 中我明白了:

It is working well in Python 3 but in Python 2.7 I get this:

sre_constants.error: bad character range

如何修复它以同时支持 Python 2.7 和 Python 3?

How can I fix it to support both, Python 2.7 and Python 3?

推荐答案

使用 r'(... 而不是 u'(... 像这样:

Use r'(... instead of u'(... like this:

re.compile(r'([\U00002600-\U000027BF\U0001F300-\U0001F64F\U0001F680-\U0001F6FF])')

另请注意,您可以在 [...]

https://regex101.com/r/WuQ3Zr/1

这篇关于Python 正则表达式:错误的字符范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆