假设我有一根线看起来像
test = 'this is a (meh) sentence that uses (random bits of meh2) and (this is silly)'
我只想提取括号内包含单词“meh”的文本。在
执行常规非贪心正则表达式以匹配括号内的任何内容:
^{pr2}$退货
['meh', 'random bits of meh2', 'this is silly']
尝试只包含第一和第二个内容:
re.findall(r'\((.*meh.*?)\)', test)
退货
['meh) sentence that uses (random bits of meh2']
我只想要一个正则表达式返回
['meh', 'random bits of meh2']
有人能帮忙吗?谢谢!在
您可以使用
[^\)]
,而不是允许所有字符,而是使用[^\)]
,其中.
现在是.
。在相关问题 更多 >
编程相关推荐