Regex failing to match inner repeatable occurrences

后端 未结 1 608
情歌与酒
情歌与酒 2020-12-12 05:09

I\'m failing to match a nested capturing group multiple times, the result only gives me the last inner capture instead.

Input string: =F2=0B=E2some te

相关标签:
1条回答
  • 2020-12-12 06:04

    You just need to access the match.Groups[2].Captures collection.

    See the regex demo

    What you need is a CaptureCollection. See this Regex.Match reference:

    Captures
    Gets a collection of all the captures matched by the capturing group, in innermost-leftmost-first order (or innermost-rightmost-first order if the regular expression is modified with the RegexOptions.RightToLeft option). The collection may have zero or more items.(Inherited from Group.)

    Here is a sample demo that outputs all the captures from Groups[2] CaptureCollection (F2, 0B, E2, C2, A3):

    var pattern = "(=([0-9A-F]{2}))+";
    var result = Regex.Matches("=F2=0B=E2some text =C2=A3", pattern)
              .Cast<Match>().Select(p => p.Groups[2].Captures)
              .ToList();
    foreach (var coll in result)
       foreach (var v in coll)
            Console.WriteLine(v);
    
    0 讨论(0)
提交回复
热议问题