C# Regex Groups, Named Group Example

Use the Groups property on a Match result. Access named groups with a string.
Regex Groups. Regex.Match returns a Match object. The Groups property on a Match gets the captured groups within the regular expression.Regex
This property is useful for extracting a part of a string from a match. It can be used with multiple captured parts. Groups can be accessed with an int or string.
An example. IndexOf and LastIndexOf are inflexible when compared to Regex.Match. Regex lets you specify substrings with a certain range of characters, such as A-Za-z0-9.

Part A: This is the input string we are matching. Notice how it contains 3 uppercase words, with no spacing in between.

Part B: We see the verbatim string literal syntax. It escapes characters differently than other string literals.

Part C: We call Match on the Regex we created. This returns a Match object. We extract the capture from this object.

String Literal

Note: It is important to use the Groups[1] syntax. The groups are indexed starting at 1, not 0.

C# program that uses Match Groups using System; using System.Text.RegularExpressions; class Program { static void Main() { // Part A: the input string we are using. string input = "OneTwoThree"; // Part B: the regular expression we use to match. Regex r1 = new Regex(@"One([A-Za-z0-9\-]+)Three"); // Part C: match the input and write results. Match match = r1.Match(input); if (match.Success) { string v = match.Groups[1].Value; Console.WriteLine("Between One and Three: {0}", v); } } } Output Between One and Three: Two
Named group. Here we use a named group in a regular expression. We then access the value of the string that matches that group with the Groups property. We use a string index key.

Here: The input string has the number 12345 in the middle of two strings. We match this in a named group called "middle."

C# program that uses named group using System; using System.Text.RegularExpressions; class Program { static void Main() { // ... Input string. string input = "Left12345Right"; // ... Use named group in regular expression. Regex expression = new Regex(@"Left(?<middle>\d+)Right"); // ... See if we matched. Match match = expression.Match(input); if (match.Success) { // ... Get group by name. string result = match.Groups["middle"].Value; Console.WriteLine("Middle: {0}", result); } // Done. Console.ReadLine(); } } Output Middle: 12345
Example 2. Alternatively we can use IndexOf and LastIndexOf. There are many small variations on this code pattern. It is more fragile. It normally requires more development effort.IndexOfLastIndexOf

Info: This code uses char positions to find the first instance of the left side string, and the last instance of the right.

Caution: It may fail in other cases. But it is likely that this version is faster.

C# program that uses IndexOf using System; class Program { static void Main() { // The input string we are using. string input = "OneTwoThree"; // Find first instance of this string. int i1 = input.IndexOf("One"); if (i1 != -1) { // Find last instance of the last string. int i2 = input.LastIndexOf("Three"); if (i2 != -1) { // Get the substring and print it. int start = i1 + "One".Length; int len = i2 - start; string bet = input.Substring(start, len); Console.WriteLine("Between One and Three: {0}", bet); } } } } Output Between One and Three: Two
Between, before, after. Regular expressions are not needed for simple parsing tasks. Consider a specialized method that can access strings between, before and after other strings.Between, Before, After
A summary. You can find a string between two delimiters of multiple characters. With Groups, we can access "captured" values with an int or string.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to