当前位置: 代码迷 >> C# >> 问个正则分组,该如何处理
  详细解决方案

问个正则分组,该如何处理

热度:34   发布时间:2016-05-05 04:25:59.0
问个正则分组
  IF THE FARE COMPONENT INCLUDES TRAVEL BETWEEN HND AIRPORT                     
  AND SEL                                                                       
      ON DIRECT FLIGHTS                                                         
    OUTBOUND -                                                                  
      PERMITTED 201PM TO MIDNIGHT DAILY ON THE FIRST                            
      INTERNATIONAL SECTOR.                                                     
    INBOUND -                                                                   
      PERMITTED 1201AM TO 200PM DAILY DEPARTURE OF LAST                         
      INTERNATIONAL SECTOR.                                                     
  OTHERWISE                                                                     
    PERMITTED 1201AM TO MIDNIGHT DAILY.   

我想分成两组
A:
  IF THE FARE COMPONENT INCLUDES TRAVEL BETWEEN HND AIRPORT                     
  AND SEL                                                                       
      ON DIRECT FLIGHTS                                                         
    OUTBOUND -                                                                  
      PERMITTED 201PM TO MIDNIGHT DAILY ON THE FIRST                            
      INTERNATIONAL SECTOR.                                                     
    INBOUND -                                                                   
      PERMITTED 1201AM TO 200PM DAILY DEPARTURE OF LAST                         
      INTERNATIONAL SECTOR.

B:                                                             
    PERMITTED 1201AM TO MIDNIGHT DAILY.

这样的正则,解析不出来。
(?<A>\s+.*)\sOTHERWISE(?<B>\s+.*)

------解决思路----------------------
static void RegexChinese()
        {
            string txt = @"IF THE FARE COMPONENT INCLUDES TRAVEL BETWEEN HND AIRPORT                     
  AND SEL                                                                       
      ON DIRECT FLIGHTS                                                         
    OUTBOUND -                                                                  
      PERMITTED 201PM TO MIDNIGHT DAILY ON THE FIRST                            
      INTERNATIONAL SECTOR.                                                     
    INBOUND -                                                                   
      PERMITTED 1201AM TO 200PM DAILY DEPARTURE OF LAST                         
      INTERNATIONAL SECTOR.                                                     
  OTHERWISE                                                                     
    PERMITTED 1201AM TO MIDNIGHT DAILY.";
            var match = Regex.Match(txt, @"(?<A>[\s\S]*)OTHERWISE(?<B>[\s\S]*)");
            if (match.Success)
            {
                Console.WriteLine("A:" + match.Groups["A"].Value);
                Console.WriteLine("=============================");
                Console.WriteLine("b:" + match.Groups["B"].Value);
            }
        }
  相关解决方案