Get named group from Match : Regex Group « Regular Expression « C# / CSharp Tutorial






using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"(\d+)*(\w)\2";
      string input = "AA";
      Match match = Regex.Match(input, pattern);

      // Get the first named group.
      Group group1 = match.Groups[1];
      Console.WriteLine("Group 1 value: {0}", group1.Success ? group1.Value : "Empty");

      // Get the second named group.
      Group group2 = match.Groups[2];
      Console.WriteLine("Group 2 value: {0}", group2.Success ? group2.Value : "Empty");

      // Get a non-existent group.
      Group group3 = match.Groups[3];
      Console.WriteLine("Group 3 value: {0}", group3.Success ? group2.Value : "Empty");
   }
}








17.4.Regex Group
17.4.1.Match Group Value
17.4.2.Get Group in a match
17.4.3.Name a Regex group
17.4.4.RegEx Group
17.4.5.Get named group from Match