C# 正则表达式 : returning a collection of results

标签 c# regex

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/

相关文章:

c# - XmlArray 序列化 - 如何使序列化程序忽略列表中项目的类名?

c# - 带背景的 WPF WindowChrome - 系统按钮被隐藏

c# - 用户控制事件的设计时查看

c# - 多应用服务器环境和 Memcached 安全

php - 正则表达式解析 imdb 页面并获取名称

c# - wcf 客户端代理的 Abort() 方法在捕获 FaultException 后不释放 session

c# - .NET 正则表达式 : negate previous character for the first character in string

c# - preg_match 到 .NET 等价物

regex - 确定一个正则表达式是否是另一个正则表达式的子集

java - Java regex是否支持Unicode?