Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正则表达式匹配多个组_C#_Regex - Fatal编程技术网

C# 正则表达式匹配多个组

C# 正则表达式匹配多个组,c#,regex,C#,Regex,下面是我试图匹配的带有正则表达式的字符串的示例: 正则表达式: ^\d{3}([0-9a-fA-F]{2}){3} 要匹配的字符串: 010 00 我的问题是-正则表达式匹配并捕获1组-字符串末尾的最终00。但是,我希望它在最后匹配所有三个00组。为什么这样不行?当然,括号应该意味着它们都是平等匹配的 我知道我可以把这三组分别打出来,但这只是一个较长字符串的简短摘录,所以这会很痛苦。我希望这将提供一个更优雅的解决方案,但似乎我的理解有所欠缺 谢谢 因为捕获组中有一个量词,所以只能看到上一次迭代中

下面是我试图匹配的带有正则表达式的字符串的示例:

正则表达式:
^\d{3}([0-9a-fA-F]{2}){3}

要匹配的字符串:
010 00

我的问题是-正则表达式匹配并捕获1组-字符串末尾的最终
00
。但是,我希望它在最后匹配所有三个
00
组。为什么这样不行?当然,括号应该意味着它们都是平等匹配的

我知道我可以把这三组分别打出来,但这只是一个较长字符串的简短摘录,所以这会很痛苦。我希望这将提供一个更优雅的解决方案,但似乎我的理解有所欠缺


谢谢

因为捕获组中有一个量词,所以只能看到上一次迭代中的捕获。不过,您很幸运,.NET(与其他实现不同)提供了一种机制,通过从所有迭代中检索捕获。从链接的文档中:

  // 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);
  }
如果将量词应用于捕获组,则CaptureCollection为每个捕获的子字符串包含一个捕获对象,并且组对象仅提供关于最后捕获的子字符串的信息

以及链接文档中提供的示例:

  // 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);
  }
//将一个句子与具有
//适用于整个组。
模式=@“(\b\w++\w{1,2})+”;
match=Regex.match(输入,模式);
Console.WriteLine(“模式:+Pattern”);
Console.WriteLine(“匹配:+Match.Value”);
WriteLine(“Match.Captures:{0}”,Match.Captures.Count);
对于(int-ctr=0;ctr
这应该适用于当前字符串。我需要一个更好的例子(更多的字符串等),看看这是否会打破这些。单词边界(\b)检查是否有任何非单词字符:

\b[0-9a-fA-F]{2}\b

啊,好的。。我认为
{3}
会以同样的方式与前一组匹配3次。所以这应该可以解决我对.NET的问题,但是对于其他的实现,有没有一种更优雅的方法,而不仅仅是重复所有的东西n次呢?不幸的是,没有,我不相信有任何其他的方法。回答很好。我没有注意到这一点(尽管阅读帮助页面时它确实敲响了警钟)。我没有意识到这一点。不幸的是,我不认为上述行为是我真正想要的。如果全部都打满了,我可能就得打字了。没有关系!非常感谢您的快速回答!这似乎对我不起作用。。我想你是想扩展到
(\b[0-9a-fA-F]{2}\b){3}
?我想我误解了你的问题,阿雄看起来他的想法是对的