简体   繁体   中英

More than One Regex Matching for a string

I have a string that needs to be parsed in Regex and the groups have to taken

Dear Mr XXXX -Your IndiGo PNR is OFWDUE- 6E 437 29May BOMBLR 0610/0750 and 6E 426 03Jun BLRBOM 1800/1940. Use our app- http://mobile.goindigo.in to check flt. status or book next time.

The regexes that I have written that I matched with this are

Dear (.*?) -Your IndiGo PNR is (.*?)- (.*? ?.*?) (.*?) (.*?) (.*?)/(.*?)\. Use our app- (.*?) to check flt\. status or book next time\.

and

Dear (.*?) -Your IndiGo PNR is (.*?)- (.*? ?.*?) (.*?) (.*?) (.*?)/(.*?) and (.*? ?.*?) (.*?) (.*?) (.*?)/(.*?)\. Use our app- (.*?) to check flt. status or book next time\.

However, it should have just matched with the latter one. Also, the groups are also very important for me as I have to extract information from them.

How do I take care of this? What changes must I make in both regexes so that in this case, only the second one is matched?

The regex you are using are too general, so they can match more strings than you intend to.

For instance Dear Mr XXXX , XXXX will be for sure a name, so you can exclude numbers from your regex.

The other regexes will match numbers only or uppercase strings, so you can be more selective and catch the right patterns. Of course how to write the regex depends also on which programming language you are using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM