Python正则表达式:仅获取一个匹配的表达式 [英] Python regex: Get only one expression to match

查看:388
本文介绍了Python正则表达式:仅获取一个匹配的表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在研究一个程序,该程序将多个正则表达式与一个语句匹配:

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

在这里只有matchObj应该是正确的匹配项,那么如何阻止其他正则表达式报告匹配项?

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

推荐答案

问题是,与第一个正则表达式匹配的每个字符串也与第二个正则表达式匹配(与at (.*?) .*匹配的任何字符串也与.*匹配.所以matchObj2 实际上是正确的匹配.

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

这篇关于Python正则表达式:仅获取一个匹配的表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆