【问题标题】:Regex: How to get a group name正则表达式:如何获取组名
【发布时间】:2009-07-22 06:17:31
【问题描述】:

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

(?<Type1>AAA)|(?<Type2>BBB)

我正在对示例字符串使用 Matches 方法,例如"AAABBBAAA",然后遍历匹配项。

我的目标是找到匹配类型,使用正则表达式匹配组,所以对于这个正则表达式它将是:

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

我找不到任何GetGroupName 方法。请帮忙。

【问题讨论】:

    标签: .net regex


    【解决方案1】:

    这是您正在寻找的东西吗?它使用Regex.GroupNameFromNumber,因此您无需知道正则表达式本身之外的组名。

    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);
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果要检索特定组名,可以使用Regex.GroupNameFromNumber 方法。

      //regular expression with a named group
      Regex regex = new Regex(@"(?<Type1>AAA)|(?<Type2>BBB)", RegexOptions.Compiled);
      
      //evaluate results of Regex and for each match
      foreach (Match m in regex.Matches("AAABBBAAA"))
      {
          //loop through all the groups in current match
          for(int x = 1; x < m.Groups.Count; x ++)
          {
              //print the names wherever there is a succesful match
              if(m.Group[x].Success)
                  Console.WriteLine(regex.GroupNameFromNumber(x));
          }
      }
      

      另外,GroupCollection 上有一个字符串索引器。 Match.Groups 属性上可访问的对象,这允许您通过名称而不是索引访问匹配中的组。

      //regular expression with a named group
      Regex regex = new Regex(@"(?<Type1>AAA)|(?<Type2>BBB)", RegexOptions.Compiled);
      
      //evaluate results of Regex and for each match
      foreach (Match m in regex.Matches("AAABBBAAA"))
      {
          //print the value of the named group
          if(m.Groups["Type1"].Success)
              Console.WriteLine(m.Groups["Type1"].Value);
          if(m.Groups["Type2"].Success)
              Console.WriteLine(m.Groups["Type2"].Value);
      }
      

      【讨论】:

        【解决方案3】:

        试试这个:

                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));
                    }    
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-10-06
          • 1970-01-01
          • 2010-11-25
          • 2012-10-21
          • 2015-05-05
          • 1970-01-01
          • 2019-03-17
          • 2011-03-03
          相关资源
          最近更新 更多