我希望能够匹配一个包含可选结尾
'other (\\w+)'
的句子。例如,正则表达式应该匹配以下两个句子,并提取单词 'things':
- The apple and other things.
- The apple is big.
(None,)
的结果。如果我删除最后一个 ?
。我会得到正确的答案。为什么?>>> re.search('\w+(?: other (\\w+))?', 'A and other things').groups()
(None,)
>>> re.search('\w+(?: other (\\w+))', 'A and other things').groups()
('things',)