Python 正则表达式 : Bad character range

标签 python regex python-2.7 python-3.x

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

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

它在 Python 3 中运行良好,但在 Python 2.7 中我得到了这个:

sre_constants.error: bad character range

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

最佳答案

使用 r'(... 而不是 u'(...,如下所示:

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

另请注意,您可以在 [...] 内指定多个范围

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

关于Python 正则表达式 : Bad character range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44314290/

相关文章:

python - 'for' 循环中 i = i + 1 和 i += 1 有什么区别?

python - 是否可以使用 lambda 函数检索 Qt Signals 的内容?

Java正则表达式

python-2.7 - 无法在 Azure LAMP shell 服务器上使用 pip 安装 Python 包

python - 在 Python 上计算三分之一

python - 合并两个 pandas 数据框并跳过右侧的公共(public)列

python - 如何将图像的 Blob 转移到白色背景?

regex - 如何仅对包含大写字母的单词进行grep

regex - Outlook:如何过滤在正文中明确提到我名字的电子邮件

python - python中的全局变量引用