Python正则表达式:仅获取一个匹配的表达式

3

我正在处理一个程序,它需要在一个语句中匹配多个正则表达式:

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
1个回答

2
问题在于,每个匹配第一个正则表达式的字符串也都匹配第二个正则表达式(任何与at (.*?) .*匹配的内容也会匹配.*)。因此,matchObj2实际上是一个正确的匹配。
如果你想区分这两种情况,你需要仅在第一个正则表达式没有匹配时应用第二个正则表达式。
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))
elif matchObj2:
   print("matchObj2.group() : ", matchObj2.group())
   print("matchObj2.group(1) : ", matchObj2.group(1))
   print("matchObj2.group(2) : ", matchObj2.group(2))
else:
   print("No match!!")

运行得非常好。回复晚了,抱歉。非常感谢! - Mayukh Nair

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接