我想从这句话中提取the cat
和another mat
:
>>> text = "the cat sat on another mat"
>>>
>>> re.findall('(the|another)\s+\w+', text)
['the', 'another']
但它不会返回后面的 cat
和 mat
。如果我将它更改为 re.findall('another\s+\w+', text)
然后它会找到那个部分,但为什么 (first thing | second thing)
工作?
(使用Python的re
模块)
最佳答案
我愿意
import re
text = "the cat sat on another mat"
re.findall('the\s+\w+|another\s+\w+', text)
结果应该是
>>> ['the cat', 'another mat']
关于python - 正则表达式只返回匹配的第一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54430412/