string='there is a article here, there will be some other article too'
String是内容,我想要'有一篇文章'
,就到此为止,不要与最后的文章匹配。
我使用了there.+article
,但它给了我完整的内容'这里有一篇文章,还有其他一些文章
。不,我不想要这个。
这就是我想要的:
'有一篇文章'
最佳答案
可以使用?
进行非贪婪匹配。
>>> string='there is a article here, there will be some other article too'
>>> import re
>>> re.match("there.+article", string)
<re.Match object; span=(0, 57), match='there is a article here, there will be some other>
>>> re.match("there.+?article", string)
<re.Match object; span=(0, 18), match='there is a article'>
关于python - 如何使用 python 正则表达式提取上下文的第一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57105231/