如从文本this is an apple , a delecious apple.中,想要匹配this is an apple.
使用.*apple会匹配出整句,我想只匹配到开头到第一个apple,如何做到?谢谢。
使用非贪婪模式,在长度后边加问号
.*?apple
import re
s='this is an apple, a delecious apple.'
print(re.search('.*?apple',s).group())
F:\2021\qa\ot2>t8
this is an apple
.*是贪婪模式,匹配尽可能多,用.*?