Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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.
C#
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;
    } 

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:
C#
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);


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
Posted
Updated 8-May-13 23:05pm
v2
Comments
Zoltán Zörgő 9-May-13 5:10am    
Please elaborate this last sentence with the pax (what's that)!
StM0n 9-May-13 5:13am    
Sorry to ask, but ymt3 didn't contain "itinerary rebooked"...
Sukanta Dey 9-May-13 6:42am    
Can you please elaborate your requirement as ymt2 and ymt3 doesn'y have "itinenary rebooked".
Plaese expain your business logic.
Jegan Thiyagesan 9-May-13 7:41am    
First, I am bit surprise to see, this information is parsed as block of string, in which some values are separated by space and some are by CR. Also there is no identification of what is what. For example the PNR code, in your regex you are looking for 6 characters starts with a space and ends with two spaces, so if the string block has another parameter with 6 characters separated by two spaces, then its doom to failure.

thes block of strings need intermediate steps to chunk them out as headers "from start to the beginning of passenger number '1.', second the passengers and their flight details such as start from '1.' and end on 'FXR', then the third chunk the itinerary and the re booking part.

Once you split the string block into three parts then it will be easier for you to do the conditional checks as you needed.

1 solution

C#
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>


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

Jegan
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900