Optional String Match in Python Regex -
i have code in python 2.7 used in finding information flight in given piece of text. come grouped (operators have standard format , fill in blanks , send message)
import re line = "jetmobile:your flight 9w448 on 14jan2015 expected leave blr @ 19:00 hrs, reach bom @ 20:42 hrs. download our mobile app http://m.jetairways.com/mobileapps booking(s), flight status, check-in , more." matchobj = re.match( r'jetmobile\:your flight (.*?) on (.*?) expected leave (.*?) @ (.*?) hrs, reach (.*?) @ (.*?) hrs\. download our mobile app (.*?) booking\(s\), flight status, check-in , 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!!"
however want match
"jetmobile:your flight 9w448 on 14jan2015 expected leave blr @ 19:00 hrs, reach bom @ 20:42 hrs. download our mobile app http://m.jetairways.com/mobileapps booking(s), flight status, check-in , more."
and
"jetmobile:your flight 9w448 on 14jan2015 expected leave blr @ 19:00 hrs, reach bom @ 20:42 hrs. download our new mobile app http://m.jetairways.com/mobileapps booking(s), flight status, check-in , more."
how do it?
make all new
optional ?
. use following::
matchobj = re.match( r'jetmobile\:your flight (.*?) on (.*?) expected leave (.*?) @ (.*?) hrs, reach (.*?) @ (.*?) hrs\. download our (?:all new )?mobile app (.*?) booking\(s\), flight status, check-in , more\.', line, re.m|re.i)
Comments
Post a Comment