Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正则表达式匹配的Unicode字符与不同的字符串行为异常_Python_Regex - Fatal编程技术网

Python 正则表达式匹配的Unicode字符与不同的字符串行为异常

Python 正则表达式匹配的Unicode字符与不同的字符串行为异常,python,regex,Python,Regex,好的,我正在对一些字符串进行unicode正则表达式匹配 这些是有问题的字符串。不是两个单独的行,而是两个单独的字符串 \u2018Mummy\u2019 Reboot May Get \u2018Mama\u2019 Director \u2018Glee\u2019 Star Grant Gustin to Play The Flash in \u2018Arrow\u2019 Season 2 我用这个正则表达式来解析unicode引号中的标题 regex = re.compile("

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

这些是有问题的字符串。不是两个单独的行,而是两个单独的字符串

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

\u2018Glee\u2019 Star Grant Gustin to Play The Flash in \u2018Arrow\u2019 Season 2
我用这个正则表达式来解析unicode引号中的标题

regex = re.compile("\\u2018[^(?!\\u2018$)]*\\u2019",re.UNICODE)
使用regex.findall()返回我

['u2018Mama\\u2019']

这引出了两个我无法理解的问题。为什么不返回\u2018,首字母在哪里

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

re.compile("'[^']*'")
它在两个字符串中都匹配。这里有什么区别?unicode正则表达式中缺少什么

先谢谢你

#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'\u2018Mummy\u2019',u'\u2018ama\u2019',u'\u2018Glee\u2019',u'\u2018Arrow\u2019']

您使用的是Python 2还是Python 3?(这会影响字符串文字的解析方式。)输入的第一个字符是“'”还是“\\”?(也就是说,您是在向我们展示使用卷曲引号打印的字符串的repr,还是它们实际上包含反斜杠?)缺少反斜杠的问题可能是包含\u,匹配字母u。您是否尝试匹配
重新启动可能会获得第一个字符串的
控制器
,然后
明星格兰特·古斯汀在第二个字符串中播放Flash
第二季
?Python 2.7,并尝试获得木乃伊、妈妈、欢乐合唱团和阿罗。由于unicode字符,我在这里出现语法错误?我试着用这个问题来改变,但还是没有运气。我宁愿保持简单,这样我就不必担心不同服务器上的编码问题等。@Jeremy Thiesen:你忘了吗#coding=utf8,这很重要,不仅仅是一条评论。
#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