2015-06-13 36 views
0

我有一个Python 2.7中的代码,用于从给定的文本中查找航班信息。他们总是来分组(运营商有一个标准的格式,只是填补空白并发送信息)Python中的可选字符串匹配正则表达式

import re 

line = "JetMobile:Your flight 9W448 on 14Jan2015 is expected to leave BLR at 19:00 hrs, reach BOM at 20:42 hrs. Download our Mobile App from http://m.jetairways.com/mobileapps for booking(s), flight status, check-in and more." 

matchObj = re.match(r'JetMobile\:Your flight (.*?) on (.*?) is expected to leave (.*?) at (.*?) hrs, reach (.*?) at (.*?) hrs\. Download our Mobile App from (.*?) for booking\(s\), flight status, check-in and more\.', 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!!" 

但是我想匹配

"JetMobile:Your flight 9W448 on 14Jan2015 is expected to leave BLR at 19:00 hrs, reach BOM at 20:42 hrs. Download our Mobile App from http://m.jetairways.com/mobileapps for booking(s), flight status, check-in and more." 

​​

如何我要这样做吗?

+0

那么你的代码有什么问题? – Kasramvd

+0

如果有“全新”,我无法匹配。所以我想改变正则表达式来匹配 –

+0

只是让它可选!在一个没有捕获的组中(?:所有新的)? – Kasramvd

回答

1

制作all new可选?。使用以下内容::

matchObj = re.match(r'JetMobile\:Your flight (.*?) on (.*?) is expected to leave (.*?) at (.*?) hrs, reach (.*?) at (.*?) hrs\. Download our (?:all new)?Mobile App from (.*?) for booking\(s\), flight status, check-in and more\.', line, re.M|re.I)