.net Regex:如何获取组名

.net Regex:如何获取组名,.net,regex,.net,Regex,我有一个.NET正则表达式,它看起来类似于: (?<Type1>AAA)|(?<Type2>BBB) (?AAA)|(?BBB) 我对一个示例字符串使用Matches方法,例如,“AAABBBAAA”,然后迭代匹配 我的目标是使用正则表达式匹配组查找匹配类型,因此对于此正则表达式,它将是: 类型1 类型2 类型1 我找不到任何GetGroupName方法。请提供帮助。如果要检索特定的组名,可以使用方法 这就是你要找的东西吗?它使用,所以您不需要知道正则表达式本身之

我有一个.NET正则表达式,它看起来类似于:

(?<Type1>AAA)|(?<Type2>BBB)
(?AAA)|(?BBB)
我对一个示例字符串使用Matches方法,例如,
“AAABBBAAA”
,然后迭代匹配

我的目标是使用正则表达式匹配组查找匹配类型,因此对于此正则表达式,它将是:

  • 类型1
  • 类型2
  • 类型1

我找不到任何
GetGroupName
方法。请提供帮助。

如果要检索特定的组名,可以使用
方法


这就是你要找的东西吗?它使用,所以您不需要知道正则表达式本身之外的组名

using System;
using System.Text.RegularExpressions;

class Test
{
    static void Main()
    {
        Regex regex = new Regex("(?<Type1>AAA)|(?<Type2>BBB)");
        foreach (Match match in regex.Matches("AAABBBAAA"))
        {
            Console.WriteLine("Next match:");
            GroupCollection collection = match.Groups;
            // Note that group 0 is always the whole match
            for (int i = 1; i < collection.Count; i++)
            {
                Group group = collection[i];
                string name = regex.GroupNameFromNumber(i);
                Console.WriteLine("{0}: {1} {2}", name, 
                                  group.Success, group.Value);
            }
        }
    }
}
使用系统;
使用System.Text.RegularExpressions;
课堂测试
{
静态void Main()
{
正则表达式正则表达式=新正则表达式((?AAA)|(?BBB)”;
foreach(regex.Matches中的匹配(“AAABBBAAA”))
{
Console.WriteLine(“下一个匹配:”);
GroupCollection=match.Groups;
//请注意,组0始终是整个匹配
for(int i=1;i
试试这个:

        var regex = new Regex("(?<Type1>AAA)|(?<Type2>BBB)");
        var input = "AAABBBAAA";
        foreach (Match match in regex.Matches(input))
        {
            Console.Write(match.Value);
            Console.Write(": ");
            for (int i = 1; i < match.Groups.Count; i++)
            {
                var group = match.Groups[i];
                if (group.Success)
                    Console.WriteLine(regex.GroupNameFromNumber(i));
            }    
        }
var regex=new regex(“(?AAA)|(?BBB)”);
var input=“aaabbbaa”;
foreach(regex.Matches中的匹配(输入))
{
Console.Write(match.Value);
控制台。写(“:”;
对于(int i=1;i
using System;
using System.Text.RegularExpressions;

class Test
{
    static void Main()
    {
        Regex regex = new Regex("(?<Type1>AAA)|(?<Type2>BBB)");
        foreach (Match match in regex.Matches("AAABBBAAA"))
        {
            Console.WriteLine("Next match:");
            GroupCollection collection = match.Groups;
            // Note that group 0 is always the whole match
            for (int i = 1; i < collection.Count; i++)
            {
                Group group = collection[i];
                string name = regex.GroupNameFromNumber(i);
                Console.WriteLine("{0}: {1} {2}", name, 
                                  group.Success, group.Value);
            }
        }
    }
}
        var regex = new Regex("(?<Type1>AAA)|(?<Type2>BBB)");
        var input = "AAABBBAAA";
        foreach (Match match in regex.Matches(input))
        {
            Console.Write(match.Value);
            Console.Write(": ");
            for (int i = 1; i < match.Groups.Count; i++)
            {
                var group = match.Groups[i];
                if (group.Success)
                    Console.WriteLine(regex.GroupNameFromNumber(i));
            }    
        }