Regex Groups, Named Group Example
This page was last reviewed on Feb 1, 2024.
Dot Net Perls
Regex Groups. In C# Regex.Match returns a Match object. The Groups property on a Match gets the captured groups within the regular expression.
Shows a 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. Regular expressions are more powerful than most string methods. Regex lets you specify substrings with a certain range of characters, such as "A-Za-z0-9."
Part 1 This is the input string we are matching. Notice how it contains 3 uppercase words, with no spacing in between.
Part 2 We see the verbatim string literal syntax. It escapes characters differently than other string literals.
String Literal
Part 3 We call Match on the Regex we created. This returns a Match object. We extract the capture from this object.
Note It is important to use the 1-based Groups syntax: the groups are indexed starting at 1, not 0.
Shows a regex
using System; using System.Text.RegularExpressions; class Program { static void Main() { // Part 1: the input string we are using. string input = "a45"; // Part 2: the regular expression we use to match. Regex regex = new Regex(@"a(\d)(\d)"); // Part 3: match the input and write results. Match match = regex.Match(input); if (match.Success) { string value1 = match.Groups[1].Value; string value2 = match.Groups[2].Value; Console.WriteLine("GROUP 1 and 2: {0}, {1}", value1, value2); } } }
GROUP 1 and 2: 4, 5
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."
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); } } }
Middle: 12345
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.
String Between
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 is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Feb 1, 2024 (edit).
© 2007-2024 Sam Allen.