括号内的非贪心正则表达式,包含tex

2024-09-25 16:30:20 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一根线看起来像

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']

有人能帮忙吗?谢谢!在


Tags: andoftest内容thatisrandomthis