如何匹配相同字符串上的两个条件以使用正则表达式获得一个输出 [英] How to match two condition on same string to get one output using regex

查看:101
本文介绍了如何匹配相同字符串上的两个条件以使用正则表达式获得一个输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,我必须找到PNR号码。要获得PNR号码,我必须检查两个条件:

乘客中的第一个应该是多个。

第二个字符串包含ITINERARY REBOOKED。

如果满足两个条件,则只从字符串中提取PNR编号,否则不返回任何内容。



我的代码,其中我满足了我的第一个条件。

I have a string in which I have to find PNR number. To get PNR number I have to check two condition:
First in Passenger should be more than one.
Second String contain ITINERARY REBOOKED.
If both condition met then extract only the PNR number from the string otherwise return nothing.

My code in which i fullfill my first condition.

public DataTable GetPnr(List<string> request) // Here we create the function for get pnr.
    {
        dt.Columns.Add("PNR", typeof(string));
        foreach (string data in request)
        {
            string item = data;
            dr = dt.NewRow();
            if (item.ToLower().Contains("itinerary rebooked") || item.ToLower().Contains("itineraryrebooked")) // Condition for operated by cases
            {
                Regex regexs = new Regex(@"(\s[A-Z0-9]{6}\s{2})"); // Regular operation for PNR.
                foreach (Match m in regexs.Matches(item))
                {
                    output = m.ToString(); // Here we store the PNR value in output string variable.
                }
                dr["PNR"] = output;
                dt.Rows.Add(dr);
            }
        }
        return dt;
    } 



现在,当我使用正则表达式为我在我的函数中使用的同一个正则表达式中的多个乘客进行chech时,这个事情并没有让我任何回报输出



这里是我的字符串,我想要检查两个条件:


Now the thing when i use regex to chech for multiple passenger in the same regex which i use in my function is doesn''t return me any output

here is my string for which i want both condition to be checked:

protected void Button1_Click(object sender, EventArgs e)
{
    List<String> YMT1 = new List<String>();
    string ymt = @"RT29WHVE

RP/NYC1S21DD/NYC1S21DD            WS/SU   6MAY13/0503Z   29WHVE //29WHVE is the PNR number                
NYC1S21DD/9525GY/6MAY13                                                         
  1.KHOON/FRANCIS   2.MEH/SAY   3.MEH/PRAY   4.MEH/MAW                          
  5.REH/LAW   6.REH/PAE   7.REH/DO   8.REH/LEE   9.REH/HEH                      
 10  US 152 T 12MAY 7 GEGPHX HK9   300P 534P 12MAY  E  US/A4PRHM                
 11  US 184 T 12MAY 7 PHXLAS HK9   815P 923P 12MAY  E  US/A4PRHM                
 12  US 392 K 13MAY 1 LASCLT HK9   115A 827A 13MAY  E  US/A4PRHM                
 13  US4286 K 13MAY 1 CLTFAY HK9   955A1050A 13MAY  E  US/A4PRHM                
 OPERATED BY SUBSIDIARY/FRANCHISE                                           
 14 MIS 1A HK9 NYC 11JAN-THANK YOU FOR YOUR BUSINESS                            
 15 AP NYC9103161516                                                            
 16 APE FRANCISKHON@GMAIL.COM                                                   
 17 TK OK06MAY/NYC1S21DD//ETU 
 FXR                                                           
 01 KHOON/FRANC*                                                                
 ITINERARY REBOOKED                                                             
 LAST TKT DTE 06MAY13 - SEE ADV PURCHASE";  

 string ymt2 = @"RP/NYC1S21DD/NYC1S21DD            UA/RM   6MAY13/0452Z       298BFB                
 NYC1S21DD/9999WS/6MAY13                                                        
 1.BELIEU/KENNETH E(ADT)   2.BELIEU/RUTH J(ADT)                               
 3  UA 646 Q 22JUN 6*PDXORD HK2   603A1150A 22JUN  E  UA/GW0LVJ               
 4  UA1735 Q 22JUN 6*ORDBWI HK2   115P 409P 22JUN  E  UA/GW0LVJ               
 5  UA 209 S 01JUL 1*BWIIAH HK2   545A 800A 01JUL  E  UA/GW0LVJ               
 6  UA 258 S 01JUL 1*IAHPDX HK2   856A1120A 01JUL  E  UA/GW0LVJ               
)>FXR                                                                                
01 BELIEU/KENNE*                                                               
NO REBOOKING REQUIRED FOR LOWEST AVAILABLE FARE                                
LAST TKT DTE 07MAY13 - SEE ADV PURCHASE";

string ymt3 = @"RP/NYC1S21DD/NYC1S21DD            WS/SU   6MAY13/0509Z   Y33ORG //Y33ORG is the PNR number               
NYC1S21DD/80948W/6MAY13                                                        
1.CORTES RIVERA/MARIA(ADT)                                              
2  UA1162 W 21JUN 5*BQNEWR HK6   150A 545A 21JUN  E  UA/GW176R               
3  UA1209 W 21JUN 5*EWRLAX HK6   700A 955A 21JUN  E  UA/GW176R               
4  UA 398 V 17JUL 3 LAXEWR HK6  1040A 705P 17JUL  E  UA/GW176R";

    YMT.Add(ymt);
    YMT.Add(ymt2);
    YMT.Add(ymt3);  
    Split sp = new Split();
    DataTable dt = sp.GetPnr(YMT);





根据我的代码我把所有那些包含ITINERARY REBOOKED的PNR但我也想要,如果我只有一个与ITINERARY REBOOKED的pax然后它不会取得像ymt3那样的PNR号码我有ITINERARY REBOOKED条件但只有一个pax所以我不想拿PNR号码



according to my code i take all those PNR which contain ITINERARY REBOOKED but i also want if i have only one pax with ITINERARY REBOOKED then it won''t fetch that PNR number like in ymt3 i have ITINERARY REBOOKED condition but only one pax so i dont want to take PNR number

推荐答案

public DataTable GetPnr(List<string> request) // Here we create the function for get pnr.
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("PNR", typeof(string));
            var output = "";
            foreach (string data in request)
            {

                var dr = dt.NewRow();
                string item = data;
                int headerEndIindex = item.IndexOf("1.", 0);
                if (headerEndIindex < 1)  // No data to extract PNR number
                {
                    return dt;
                }
                string header = item.Substring(0, headerEndIindex - 1);

                int contentEndIindex = item.IndexOf("FXR", headerEndIindex - 1);
                string contents = "";
                string footer = "";
                if (contentEndIindex > 0)  // there is data to check for itinerary 
                {
                    contents = item.Substring(headerEndIindex - 1, contentEndIindex - 1);
                    footer = item.Substring(contentEndIindex - 1);
                }
                if (!string.IsNullOrEmpty(contents)) // the content is not empty to check the number of passengers
                {
                    string pattern = @"(\d?\.)";  // count the number of passengers
                    System.Text.RegularExpressions.MatchCollection matchCollection = new System.Text.RegularExpressions.Regex(pattern).Matches(contents);

                    if (matchCollection.Count > 2) // more than two passengers
                    {
                        if (!string.IsNullOrEmpty(footer))  // the footer is not empty to check for itinerary
                        {
                            pattern = @"((itinerary|ITINERARY)\s*(rebooked|REBOOKED))";
                            System.Text.RegularExpressions.Match match = new System.Text.RegularExpressions.Regex(pattern).Match(footer);
                            if (match.Success) // there is a re booking
                            {
                                // get the PNR from header
                                // \s([A-Z0-9]{6})\s
                                pattern = @"\s([A-Z0-9]{6})\s";
                                match = new System.Text.RegularExpressions.Regex(pattern).Match(header);
                                if (match.Success)
                                {
                                    output = match.Value.Trim();
                                }
                                else
                                {
                                    // NO PNR code found
                                }
                                dr["PNR"] = output;
                                dt.Rows.Add(dr);
                            }
                        }                        
                    }                    
                }
            }
            return dt;
        }</string>





尝试这个,这是一个简单的迭代,并且有评论告诉你正在做什么。



Jegan



try this, this is a simple iteration, and there are comments telling you what is being done.

Jegan


这篇关于如何匹配相同字符串上的两个条件以使用正则表达式获得一个输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆