1

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

乘客中的第一位应该不止一个。

第二个字符串包含 ITINERARY REBOOKED。

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

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

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;
} 

现在,当我使用正则表达式在我的函数中使用的同一个正则表达式中检查多个乘客时,不会返回任何输出

这是我希望检查两个条件的字符串:

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 的人,那么它不会像在 ymt3 中那样获取那个 PNR 号码我有 ITINERARY REBOOKED 条件但只有一个人所以我不想取 PNR 号码

4

1 回答 1

1

试试这个代码...

 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();

            Regex regex = new Regex(@"(\d+\.[a-zA-Z]\S(.+))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            foreach (Match m in regex.Matches(item))
            {
                name = m.ToString();
            }

            if((item.ToLower().Contains("itinerary rebooked") || item.ToLower().Contains("itineraryrebooked"))&&name!=null) // 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;
    }`enter code here`
于 2013-05-09T11:49:43.880 回答