Rub*_*ert 2 python regex string python-2.7
When I use a regex in Python 2.x containing two or more groups, re.findall() returns a list of n-tuples for the n groups contained by the regex. I know it's supposed to work like this but is there any way to circumvent this behavior?
For instance, when I run
import re
sentence = 'We also learned that the !!probability of an outcome is its relative frequency over endless repetitions of the experiment. '
print re.findall(r'[Pp]robabilit(y|ies)',sentence)
Run Code Online (Sandbox Code Playgroud)
它只返回['y']。但是,我想probability返回。对于包含“概率”的其他句子,我想probabilities返回,依此类推。
re.findall存在一个或多个组时更改的行为。它返回一个组列表;如果有多个组,则列出一个元组。
通过将该组设置为非捕获组,可以得到所需的内容:
>>> re.findall(r'[Pp]robabilit(?:y|ies)',sentence)
['probability']
Run Code Online (Sandbox Code Playgroud)
或者使用re.finditer和列表理解:
>>> [m.group() for m in re.finditer(r'[Pp]robabilit(y|ies)',sentence)]
['probability']
Run Code Online (Sandbox Code Playgroud)