Python regex: Get only one expression to match

梦想的初衷 提交于 2020-05-09 05:52:06

问题


So I'm wrestling with a program that matches multiple regular expressions against one statement:

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!!")

Now, I want only one regex to match at a time, like this:

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

Instead, both the regexes match up to the statement, like this:

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

Only matchObj should be a proper match here, so how do I stop the other regexes from reporting a match?


回答1:


The problem is that every string matching the first regex also matches the second one (anything that matches at (.*?) .* also matches .*. So matchObj2 is in fact a proper match.

If you want to distinguish these two situations, you need to apply the second regex if and only if the first one produces no match.

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!!")


来源:https://stackoverflow.com/questions/37030431/python-regex-get-only-one-expression-to-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!