2015-06-13 4 views
0

У меня есть код в Python 2.7, который используется для поиска информации из полета в данном фрагменте текста. Они всегда приходят сгруппированы (операторы имеют стандартный формат и просто заполнить пробелы и отправить сообщение)Дополнительное совпадение строк в Python Regex

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." 

, а также

"JetMobile:Your flight 9W448 on 14Jan2015 is expected to leave BLR at 19:00 hrs, reach BOM at 20:42 hrs. Download our all new 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) 
Смежные вопросы