简体   繁体   English

一个字符串匹配多个正则表达式

[英]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. 例如, 尊敬的XXXX先生 ,XXXX肯定是名称,因此您可以从正则表达式中排除数字。

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. 当然,如何编写正则表达式还取决于您所使用的编程语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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