正则表达式匹配Unicode字符与不同字符串的奇怪行为

2024-10-03 11:22:43 发布

您现在位置:Python中文网/ 问答频道 /正文

好的,我正在对一些字符串进行unicode正则表达式匹配。在

这些是有问题的弦。不是两条分开的线,而是两条分开的线。在

\u2018Mummy\u2019 Reboot May Get \u2018Mama\u2019 Director

\u2018Glee\u2019 Star Grant Gustin to Play The Flash in \u2018Arrow\u2019 Season 2

我使用这个正则表达式来解析出以unicode引号括起来的标题。在

^{pr2}$

使用正则表达式findall()还我

['u2018Mama\\u2019']

以及

['u2018Glee\\u2019', 'u2018Arrow\\u2019']

这就引出了两个我想不通的问题。为什么不返回\u2018,首字母在哪里?在

第二,有什么不同。我看不见。最后,我将\u2018和\u2019替换为'。 然后使用这个正则表达式。在

re.compile("'[^']*'")

它在两个字符串中都匹配。这有什么区别?unicode正则表达式中缺少什么?在

提前谢谢你。在


Tags: 字符串getunicodemaystardirectorrebootgrant
1条回答
网友
1楼 · 发布于 2024-10-03 11:22:43
#coding=utf8

import re

s=u'''\u2018Mummy\u2019 Reboot May Get \u2018Mama\u2019 Director
\u2018Glee\u2019 Star Grant Gustin to Play The Flash in \u2018Arrow\u2019 Season 2'''
print s
regex = re.compile(ur"‘[^(?!‘$)]*’",re.UNICODE)
m = regex.findall(s)
print m

[u'\u2018mmy\u2019',u'\u2018Mama\u2019',u'\u2018Glee\u2019',u'\u2018Arrow\u2019']

相关问题 更多 >