[英]How to match two condition on same string to get one output using regex
I have a string in which I have to find PNR number. 我有一个必须在其中查找PNR编号的字符串。 To get PNR number I have to check two condition: 要获取PNR编号,我必须检查两个条件:
First in Passenger should be more than one. 乘客中的头等舱应该超过一个。
Second String contain ITINERARY REBOOKED. 第二个字符串包含ITINERARY REBOOKED。
If both condition met then extract only the PNR number from the string otherwise return nothing. 如果两个条件都满足,则仅从字符串中提取PNR号,否则不返回任何内容。
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;
}
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);
}
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 根据我的代码,我将所有包含ITINERARY REBOOKED的PNR都带走,但是我也想如果我只有一个人带ITINERARY REBOOKED,那么它将不会像ymt3那样获取该PNR号,我有ITINERARY REBOOKED条件,但是只有一个pax,所以我没有要取PNR号码
Try this code... 试试这个代码...
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`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.