简体   繁体   English

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

[英]python regex: re.findall() not behaving as expected with alternation

I have the following code: 我有以下代码:

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))

and it returns: ['goodbye'] where I had expected it to return the full sentence - in fact, using exp.search(testString) it had picked out the rest of the sentence correctly. 它返回: ['goodbye']我曾期望它返回完整句子的地方-实际上,使用exp.search(testString)它正确地选择了句子的其余部分。 Why then, was the complete match not displayed? 为什么然后不显示完全匹配?

Thank you for your time. 感谢您的时间。

(...) in a regular expression defines a capturing group. (...)在正则表达式中定义捕获组。

re.findall returns the content of the capturing groups if your expression defines any. 如果您的表达式定义了任何内容,则re.findall返回捕获组的内容。

You can make it a non-capturing group (?:hello|goodbye) to avoid this. 您可以将其设置为非捕获组(?:hello|goodbye)以避免此情况。 See What is a non-capturing group? 请参阅什么是非捕获组?

khelwood has explained why findall() behaves this way. 凯尔伍德解释了为什么findall()会表现为这种方式。 If you want to catch the whole match without changing the regex, use 如果您想在不更改正则表达式的情况下抓住整个比赛,请使用

exp.group(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM