Click here to Skip to main content
15,894,540 members

How to match two condition on same string to get one output using regex

amitesh1989 asked:

Open original thread
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
Tags: C#, ASP.NET4.0, Regular Expressions

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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