Regex: Repeated capturing groups

后端 未结 3 2403
清歌不尽
清歌不尽 2020-11-28 13:03

I have to parse some tables from an ASCII text file. Here\'s a partial sample:

QSMDRYCELL   11.00   11.10   11.00   11.00    -.90      11     11000     1.212         


        
3条回答
  •  天命终不由人
    2020-11-28 13:47

    In C# (modified from this example):

    string input = "QSMDRYCELL   11.00   11.10   11.00   11.00    -.90      11     11000     1.212";
    string pattern = @"^(\S+)\s+(\s+[\d.-]+){8}$";
    Match match = Regex.Match(input, pattern, RegexOptions.MultiLine);
    if (match.Success) {
       Console.WriteLine("Matched text: {0}", match.Value);
       for (int ctr = 1; ctr < match.Groups.Count; ctr++) {
          Console.WriteLine("   Group {0}:  {1}", ctr, match.Groups[ctr].Value);
          int captureCtr = 0;
          foreach (Capture capture in match.Groups[ctr].Captures) {
             Console.WriteLine("      Capture {0}: {1}", 
                               captureCtr, capture.Value);
             captureCtr++; 
          }
       }
    }
    

    Output:

    Matched text: QSMDRYCELL   11.00   11.10   11.00   11.00    -.90      11     11000     1.212
    ...
        Group 2:      1.212
             Capture 0:  11.00
             Capture 1:    11.10
             Capture 2:    11.00
    ...etc.
    

提交回复
热议问题