Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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 Regex不返回任何结果_Python_Regex - Fatal编程技术网

Python Regex不返回任何结果

Python Regex不返回任何结果,python,regex,Python,Regex,我已经编写了一个正则表达式,可以从unicode字符串中提取年周期(例如,2014-2015年)。正则表达式中的\d{1,2}\^表示月份代码,该代码可能存在,也可能不存在 无论如何,以下是我编写的代码: # -*- coding: utf-8 -*- import re list_elem = '''Frank P. Smith (1886–1888) Edgar Grant Sisson (1914–1917) Douglas Z. Doty (1917–1918) {{Ray Long

我已经编写了一个正则表达式,可以从unicode字符串中提取年周期(例如,2014-2015年)。正则表达式中的
\d{1,2}\^
表示月份代码,该代码可能存在,也可能不存在

无论如何,以下是我编写的代码:

# -*- coding: utf-8 -*-

import re
list_elem = '''Frank P. Smith (1886–1888)
Edgar Grant Sisson (1914–1917)
Douglas Z. Doty (1917–1918)
 {{Ray Long}} (1918–1931)

Harry Payne Burton (1931–1942)
Frances Whiting (1942–1945)
Arthur Gordon (1946–1948)'''

period_regex = ur'(\d{1,2}\^)?\s?\d{4}\s?(–|-)\s?(\d{1,2}\^)?\s?\d{4}'  #regex for checking if its a single year or period

#checking if a normal regex works
print re.search(r'W', list_elem, flags=re.IGNORECASE)
print re.findall(r'W\w+', list_elem, flags=re.IGNORECASE)

#main regex
print re.search(period_regex, list_elem, flags=re.IGNORECASE)
print re.findall(period_regex, list_elem, flags=re.IGNORECASE)
输出为:

<_sre.SRE_Match object at 0x7f8bfd1b5510>
['Whiting']
None
[]

['Whiting']
没有一个
[]
我写的正则表达式似乎工作正常。以下是指向regex的链接:


然而,当我运行我的程序时,我仍然得到一个空匹配。你知道我做错了什么吗?

在打印语句中添加'u'作为前缀在
list\u elem
.encode('utf8')
应该可以解决你的问题。比如说,

print re.search(period_regex, list_elem, flags=re.IGNORECASE).group(0).encode('utf8')

这里的主要问题是,因为您使用的是Unicode文本,所以输入字符串也应该是Unicode。这可以通过在字符串文本中添加
u
前缀来解决

但是,
re.findall
存在一个问题,即只有在模式中定义了捕获组内容时才返回捕获组内容。由于您的模式包含捕获组,因此将得到一个元组列表。因此,您需要将所有捕获组转换为非捕获组

如果需要打印值,可能需要对值进行
.encode(“utf8”)
编码

见:

结果:

<_sre.SRE_Match object at 0x2b05baf525e0>
[u'Whiting']
1886–1888
1886–1888
1914–1917
1917–1918
1918–1931
1931–1942
1942–1945
1946–1948

[u'Whiting']
1886–1888
1886–1888
1914–1917
1917–1918
1918–1931
1931–1942
1942–1945
1946–1948
注意
list_elem=u''Frank
中的
u”“”
,所有
在模式中被替换为
(?:
),使组不被捕获


另外,
[–-]
(?:–-)
相同,但更简短、更高效。

可能与Unicode有关。这里,.BTW,我认为如果您只需要获得所有匹配项,就需要将所有捕获组替换为非捕获组。例如
r'(?:\d{1,2}^)\s?\d{4}\s?[-\s?(?:\d{1,2})s?\d}4}“
@WiktorStribiżew是的,这是一个更好的正则表达式,符合我的要求,但我仍然无法在我的机器上找到任何匹配项。有没有办法解决与unicode相关的问题?你需要通过在其声明中添加
u”“
前缀,使
列表元素成为unicode字符串。请参阅。@WiktorStribiżew我这样做了,仍然没有结果:(看,它在这里起作用了-。这正是我一直在寻找的答案。也感谢您改进了正则表达式:)
<_sre.SRE_Match object at 0x2b05baf525e0>
[u'Whiting']
1886–1888
1886–1888
1914–1917
1917–1918
1918–1931
1931–1942
1942–1945
1946–1948