string: "<something><or><other>"
regex pattern: "<(\w+)><(\w+)><(\w+)>"
如何进行正则表达式调用以返回包含括号之间所有内容的结果集合?例如,我想要一个 {"something", "or", "other"} 的结果集。
对于奖励积分,这叫什么?捕获?抓团?某种模板?我觉得如果我知道正确的术语,我就可以搜索它。
谢谢。
最佳答案
它们通常被称为捕获组,您可以获得如下所示的捕获:
Regex regex = new Regex(@"some (pattern)");
Match m = regex.Match("some pattern");
foreach (Capture c in m.Groups) {
Console.WriteLine(c.Value); // write the value to the console "pattern"
}
m.Groups.Count
会让您知道有多少组匹配,m.Groups[0]
将始终是完整的匹配文本。
关于C# 正则表达式 : returning a collection of results,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1445321/