【问题标题】:How do I get the name of captured groups in a C# Regex?如何在 C# 正则表达式中获取捕获组的名称?
【发布时间】:2010-11-25 18:00:03
【问题描述】:

有没有办法在 C# 中获取捕获组的名称?

string line = "No.123456789  04/09/2009  999";
Regex regex = new Regex(@"(?<number>[\d]{9})  (?<date>[\d]{2}/[\d]{2}/[\d]{4})  (?<code>.*)");

GroupCollection groups = regex.Match(line).Groups;

foreach (Group group in groups)
{
    Console.WriteLine("Group: {0}, Value: {1}", ???, group.Value);
}

我想得到这个结果:

群组:[我不知道这里应该去什么],价值:123456789 04/09/2009 999 组:号码,值:123456789 组:日期,值:04/09/2009 组:代码,值:999

【问题讨论】:

    标签: c# regex


    【解决方案1】:

    使用GetGroupNames 获取表达式中的组列表,然后迭代这些组,使用名称作为键进入组集合。

    例如,

    GroupCollection groups = regex.Match(line).Groups;
    
    foreach (string groupName in regex.GetGroupNames())
    {
        Console.WriteLine(
           "Group: {0}, Value: {1}",
           groupName,
           groups[groupName].Value);
    }
    

    【讨论】:

    • 谢谢!正是我想要的。我从没想过这会在 Regex 对象中:(
    【解决方案2】:

    最简洁的方法是使用此扩展方法:

    public static class MyExtensionMethods
    {
        public static Dictionary<string, string> MatchNamedCaptures(this Regex regex, string input)
        {
            var namedCaptureDictionary = new Dictionary<string, string>();
            GroupCollection groups = regex.Match(input).Groups;
            string [] groupNames = regex.GetGroupNames();
            foreach (string groupName in groupNames)
                if (groups[groupName].Captures.Count > 0)
                    namedCaptureDictionary.Add(groupName,groups[groupName].Value);
            return namedCaptureDictionary;
        }
    }
    


    一旦有了这个扩展方法,你就可以得到这样的名字和值:

        var regex = new Regex(@"(?<year>[\d]+)\|(?<month>[\d]+)\|(?<day>[\d]+)");
        var namedCaptures = regex.MatchNamedCaptures(wikiDate);
    
        string s = "";
        foreach (var item in namedCaptures)
        {
            s += item.Key + ": " + item.Value + "\r\n";
        }
    
        s += namedCaptures["year"];
        s += namedCaptures["month"];
        s += namedCaptures["day"];
    

    【讨论】:

      【解决方案3】:

      从 .NET 4.7 开始,有Group.Name 属性available

      【讨论】:

        【解决方案4】:

        您应该使用GetGroupNames();,代码将如下所示:

            string line = "No.123456789  04/09/2009  999";
            Regex regex = 
                new Regex(@"(?<number>[\d]{9})  (?<date>[\d]{2}/[\d]{2}/[\d]{4})  (?<code>.*)");
        
            GroupCollection groups = regex.Match(line).Groups;
        
            var grpNames = regex.GetGroupNames();
        
            foreach (var grpName in grpNames)
            {
                Console.WriteLine("Group: {0}, Value: {1}", grpName, groups[grpName].Value);
            }
        

        【讨论】:

          【解决方案5】:

          Regex 类是这方面的关键!

          foreach(Group group in match.Groups)
          {
              Console.WriteLine("Group: {0}, Value: {1}", regex.GroupNameFromNumber(group.Index), group.Value);
          }
          

          http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.groupnamefromnumber.aspx

          【讨论】:

          • 这是不正确的,group.Index是原文中匹配组开始的位置。不是正则表达式中组的“索引”。
          猜你喜欢
          • 2013-10-06
          • 1970-01-01
          • 1970-01-01
          • 2021-08-02
          • 2015-07-24
          • 2019-10-05
          • 2015-02-11
          相关资源
          最近更新 更多