Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.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:re.findall()的行为与alternation的预期不符_Python_Regex - Fatal编程技术网

python regex:re.findall()的行为与alternation的预期不符

python regex:re.findall()的行为与alternation的预期不符,python,regex,Python,Regex,我有以下代码: testREString = '(hello | goodbye) \s darkness \s my \s old \s friend' testString = 'sound of silence: goodbye darkness my old friend' exp = re.compile(testREString, re.VERBOSE) print(exp.findall(testString)) 然后它返回:['bye']我希望它返回完整的句子-事实上,使用ex

我有以下代码:

testREString = '(hello | goodbye) \s darkness \s my \s old \s friend'
testString = 'sound of silence: goodbye darkness my old friend'
exp = re.compile(testREString, re.VERBOSE)
print(exp.findall(testString))
然后它返回:
['bye']
我希望它返回完整的句子-事实上,使用
exp.search(testString)
它已经正确地挑出了句子的其余部分。那么,为什么没有显示完整的匹配

感谢您的时间。

(…)
在正则表达式中定义捕获组

如果表达式定义了捕获组,则返回捕获组的内容


您可以将其设置为非捕获组
(?:hello |再见)
,以避免出现这种情况。请参见

khelwood解释了findall()为何会有这种行为。如果希望在不更改正则表达式的情况下捕获整个比赛,请使用

exp.group(0)

因为
(…)
是一个捕获组,如果您定义了任何捕获组,则返回捕获组的内容。将其设置为非捕获组
(?:…)
要使其成为非捕获组,只需在括号的开头添加
?: