我正在处理一个程序,它需要在一个语句中匹配多个正则表达式:
import re
line = "Remind me to pick coffee up at Autostrada at 4:00 PM"
matchObj = re.match( r'Remind me to (.*) at (.*?) at (.*?) .*', line, re.M|re.I|re.M)
matchObj2 = re.match( r'Remind me to (.*) at (.*?) .*', line, re.M|re.I)
if matchObj:
print("matchObj.group() : ", matchObj.group())
print("matchObj.group(1) : ", matchObj.group(1))
print("matchObj.group(2) : ", matchObj.group(2))
print("matchObj.group(3) :", matchObj.group(3))
else:
print("No match!!")
if matchObj2:
print("matchObj2.group() : ", matchObj2.group())
print("matchObj2.group(1) : ", matchObj2.group(1))
print("matchObj2.group(2) : ", matchObj2.group(2))
else:
print("No match!!")
现在,我只想一次匹配一个正则表达式,像这样:
matchObj.group() : Remind me to pick coffee up at Autostrada at 4:00 PM
matchObj.group(1) : pick coffee up
matchObj.group(2) : Autostrada
matchObj.group(3) : 4:00
相反,这两个正则表达式都与语句匹配,如下所示:
matchObj.group() : Remind me to pick coffee up at Autostrada at 4:00 PM
matchObj.group(1) : pick coffee up
matchObj.group(2) : Autostrada
matchObj.group(3) : 4:00
matchObj2.group() : Remind me to pick coffee up at Autostrada at 4:00 PM
matchObj2.group(1) : pick coffee up at Autostrada
matchObj2.group(2) : 4:00
只有matchObj
应该是一个合适的匹配,那么我如何阻止其他正则表达式报告匹配?
.*
是一个非常贪婪的模式。除非你为这种情况制定限制规则,否则无法帮助你。当然,你可以尝试使用可控贪婪标记,例如^Remind me to ((?:(?! at ).)*) at ((?:(?! at ).)*)$
,但我不确定它是否适用于你。 - Wiktor Stribiżew