因为您在捕获组上有一个量词,所以您只能看到上次迭代的捕获。不过幸运的是,.NET(与其他实现不同)提供了一种通过CaptureCollection 类从所有迭代中检索捕获的机制。从链接的文档中:
如果将量词应用于捕获组,则 CaptureCollection 为每个捕获的子字符串包含一个 Capture 对象,并且 Group 对象仅提供有关最后捕获的子字符串的信息。
以及链接文档中提供的示例:
// Match a sentence with a pattern that has a quantifier that
// applies to the entire group.
pattern = @"(\b\w+\W{1,2})+";
match = Regex.Match(input, pattern);
Console.WriteLine("Pattern: " + pattern);
Console.WriteLine("Match: " + match.Value);
Console.WriteLine(" Match.Captures: {0}", match.Captures.Count);
for (int ctr = 0; ctr < match.Captures.Count; ctr++)
Console.WriteLine(" {0}: '{1}'", ctr, match.Captures[ctr].Value);
Console.WriteLine(" Match.Groups: {0}", match.Groups.Count);
for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
{
Console.WriteLine(" Group {0}: '{1}'", groupCtr, match.Groups[groupCtr].Value);
Console.WriteLine(" Group({0}).Captures: {1}",
groupCtr, match.Groups[groupCtr].Captures.Count);
for (int captureCtr = 0; captureCtr < match.Groups[groupCtr].Captures.Count; captureCtr++)
Console.WriteLine(" Capture {0}: '{1}'", captureCtr, match.Groups[groupCtr].Captures[captureCtr].Value);
}