0

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

  1. 乘客中的第一位应该不止一个。
  2. 第二个字符串包含 ITINERARY REBOOKED。

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

输入字符串示例:

 string First=@"RT29WHVE

 RP/NYC1S21DD/NYC1S21DD            WS/SU   6MAY13/0503Z   29WHVE // This is PNR number                
  NYC1S21DD/9525GY/6MAY13                                                         
 1.KHOON/FRANCIS   2.MEH/SAY   3.MEH/PRAY   4.MEH/MAW      //Passenger more than one                    
 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//ETUS                                                  
 18 SSR DOCS US HK1 ////09NOV83/M//KHOON/FRANCIS/P1                             
 19 SSR DOCS US HK1 ////01JAN92/M//MEH/SAY/P2                                   
 20 SSR DOCS US HK1 ////01JAN62/M//MEH/PRAY/P3                                  
 21 SSR DOCS US HK1 ////01JAN96/M//MEH/MAW/P4                                   
 22 SSR DOCS US HK1 ////15APR89/M//REH/LAW/P5                                   
 23 SSR DOCS US HK1 ////11NOV97/M//REH/PAE/P6                                   
 24 SSR DOCS US HK1 ////01JAN59/M//REH/DO/P7                                    
 25 SSR DOCS US HK1 ////05OCT88/M//REH/LEE/P8  
)>SP1                                                               
RP/NYC1S21DD/NYC1S21DD            WS/SU   6MAY13/0503Z   XXXXXX                
 1.KHOON/FRANCIS                                                              
 2  US 152 T 12MAY 7 GEGPHX HK1   300P 534P 12MAY  E  US/A4PRHM               
 3  US 184 T 12MAY 7 PHXLAS HK1   815P 923P 12MAY  E  US/A4PRHM               
 4  US 392 K 13MAY 1 LASCLT HK1   115A 827A 13MAY  E  US/A4PRHM               
 5  US4286 K 13MAY 1 CLTFAY HK1   955A1050A 13MAY  E  US/A4PRHM               
 OPERATED BY SUBSIDIARY/FRANCHISE                                          
 6 MIS 1A HK1 NYC 11JAN-THANK YOU FOR YOUR BUSINESS                           
 7 AP NYC9103161516                                                           
 8 APE FRANCISKHON@GMAIL.COM                                                  
 9 TK OK06MAY/NYC1S21DD//ETUS                                                 
10 SSR DOCS US HK1 ////09NOV83/M//KHOON/FRANCIS                               
11 OSI US TKT NO 037-7244752856                                               
12 OSI US TKT NO 037-7244752857                                               
13 OSI US TKT NO 037-7244752858                                               
14 OSI US TKT NO 037-7244752859                                               
15 OSI US TKT NO 037-7244752860                                               
16 OSI US TKT NO 037-7244752861                                               
17 OSI US TKT NO 037-7244752862                                               
18 OSI US TKT NO 037-7244752863     
                                                          FXR                                                                                                                                 
01 KHOON/FRANC*                                                                
 ITINERARY REBOOKED         //It contain ITINERATY REBOOKED so fetch that PNR  numebr                                                    
 LAST TKT DTE 06MAY13 - SEE ADV PURCHASE                                        
 ------------------------------------------------------------                   
     AL FLGT  BK   DATE  TIME  FARE BASIS      NVB  NVA   BG                   
  GEG                                                                           
 XPHX US   152 K *  12MAY 1500  KXA0NL6P        12MAY12MAY 0P                   
  LAS US   184 K *  12MAY 2015  KXA0NL6P        12MAY12MAY 0P                   
 XCLT US   392 K    13MAY 0115  KXA7NJ6         13MAY13MAY 0P                   
 FAY US  4286 K    13MAY 0955  KXA7NJ6         13MAY13MAY 0P                   

  USD   290.24      12MAY13GEG US X/PHX US LAS144.19KXA0NL6P                     
              US X/CLT US FAY146.05KXA7NJ6 USD290.24END                    
  USD    21.76US    ZP GEG3.90PHX3.90LAS3.90CLT3.90XT USD                        
  USD     5.00AY    15.60ZP USD 9.00XF GEG4.50PHX4.50                            
  USD    24.60XT                                                                 
  USD   341.60                                                                   
  BG CXR: US                                                                     
  PRICED WITH VALIDATING CARRIER US - REPRICE IF DIFFERENT VC                    
>                                                 PAGE  2/ 3                   
>";

     string Second=@"rt298BFB
 --- TST RLR MSC ---                                                            
   RP/NYC1S21DD/NYC1S21DD            UA/RM   6MAY13/0452Z   298BFB // This is PNR   number               
 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               
 7 MIS 1A HK2 NYC 11JAN-THANK YOU FOR YOUR BUSINESS                           
 8 MCO XD ARC 06MAY/USD 44.00/*SERVICE FEE/P1                                 
 9 AP NYC5093952712                                                           
 10 API CHEAPOAIR, 213 WEST 35TH STREET, SUITE 1201, NEW YORK,                 
   NY 10001                                                                
 11 TK OK06MAY/NYC1S21DD//ETUA                                                 
 12 SSR SEAT UA KK2 PDXORD/36AN,P1/36BN,P2/RB/S3                               
 13 SSR SEAT UA KK2 ORDBWI/31FN,P1/31EN,P2/RB/S4                               
)>sp1
 --- TST RLR MSC ---                                                            
 -ASSOCIATE PNR-                                                                
 RP/NYC1S21DD/NYC1S21DD            UA/RM   6MAY13/0452Z   XXXXXX                
  1.BELIEU/KENNETH E(ADT)                                                      
  2  UA 646 Q 22JUN 6*PDXORD HK1   603A1150A 22JUN  E  UA/GW0LVJ               
  3  UA1735 Q 22JUN 6*ORDBWI HK1   115P 409P 22JUN  E  UA/GW0LVJ               
  4  UA 209 S 01JUL 1*BWIIAH HK1   545A 800A 01JUL  E  UA/GW0LVJ               
  5  UA 258 S 01JUL 1*IAHPDX HK1   856A1120A 01JUL  E  UA/GW0LVJ               
  6 MIS 1A HK1 NYC 11JAN-THANK YOU FOR YOUR BUSINESS                           
  7 MCO XD ARC 06MAY/USD 44.00/*SERVICE FEE                                    
  8 AP NYC5093952712                                                           
  9 API CHEAPOAIR, 213 WEST 35TH STREET, SUITE 1201, NEW YORK,                 
   NY 10001                                                                
  10 TK OK06MAY/NYC1S21DD//ETUA                                                 
  11 SSR SEAT UA KK1 PDXORD/36AN,P1/RB/S2                                             
   )>FXR
    FXR                                                                              
 01 BELIEU/KENNE*                                                               
  NO REBOOKING REQUIRED FOR LOWEST AVAILABLE FARE //it contain no booking so dont want that PNR number                               
 LAST TKT DTE 07MAY13 - SEE ADV PURCHASE                                        
 ------------------------------------------------------------                   
      AL FLGT  BK   DATE  TIME  FARE BASIS      NVB  NVA   BG                   
PDX                                                                           
XCHI UA   646 Q    22JUN 0603  QA7KN           22JUN22JUN 0P                   
BWI UA  1735 Q    22JUN 1315  QA7KN           22JUN22JUN 0P                   
XHOU UA   209 S    01JUL 0545  SA7KN           01JUL01JUL 0P                   
PDX UA   258 S    01JUL 0856  SA7KN           01JUL01JUL 0P                   

USD   547.91      22JUN13PDX UA X/CHI UA BWI323.72QA7KN UA                     
              X/HOU UA PDX224.19SA7KN USD547.91END                         
USD    41.09US    ZP PDX3.90ORD3.90BWI3.90IAH3.90XT USD                        
USD    10.00AY    15.60ZP USD 16.50XF PDX4.50ORD4.50BWI4.50                    
USD    32.10XT    IAH3.00                                                      
USD   631.10                                                                   
BG CXR: UA                                                                     
PRICED WITH VALIDATING CARRIER UA - REPRICE IF DIFFERENT VC>";

我目前使用的正则表达式是(\s[A-Z0-9]{6}\s{2})(?=(.*ITINERARY.REBOOKED.?)) 它会给我包含 ITINERARY REBOOKED 的 PNR 号码,但我也想要第一个条件。

4

1 回答 1

0

正如对问题的评论开始解释的那样:这里有很多歧义:如果 PNR 是 6 个字符的字母数字字符串,那么输入字符串中有很多东西可以匹配,所以关于 PNR 格式的更详细标准看起来确实需要应用的地方。

假设(基于评论和一些猜测)标准可以更完整地指定为:

  • PNR 是六位大写字母数字代码。
  • 它出现在输入的第三行末尾
    • 评论说得很清楚,但很明显它是示例中的第三个。
    • 文本开头//已由提问者添加,但实际上并不是输入的一部分。

仅当字符串包含文字 text 时才应处理该字符串ITINERARY REBOOKED

因此不需要正则表达式:

bool TryGetPnr(string input, out string pnr) {
  pnr = String.Empty;

  if (input.IndexOf("ITINERARY REBOOKED", StringComparison.Ordinal) == -1) {
    return false;
  }

  // Get the 3rd line (index 2) without building too much of a temporary array
  // This assumes \r\n line endings.
  var line3 = input.Split(new [] { Environment.NewLine }, 4, StringSplitOptions.None)[2];

  pnr = line3.Substring(line3.Length - 6);
  return true;
}

但是您需要首先仔细指定您想要的内容,以确保您正确提取您想要的内容。目前问题的格式规范充其量是模糊的。

于 2013-05-07T11:40:25.710 回答