0
0
CsharpHow-ToBeginner · 3 min read

How to Use Regex Groups in C# for Pattern Matching

In C#, you use Regex groups by placing parts of your pattern inside parentheses (). You can then access these groups using the Groups property of a Match object to extract specific parts of the matched text.
📐

Syntax

Regex groups are created by enclosing a part of the pattern in parentheses (). Each group is numbered starting from 1, while group 0 is the entire match. You use Regex.Match to find a match and then access groups via Match.Groups[index].

  • (): Defines a capturing group.
  • Match.Groups[0]: The full matched string.
  • Match.Groups[1]: The first captured group.
  • Match.Groups[n]: The nth captured group.
csharp
using System.Text.RegularExpressions;

string pattern = "(\w+)@(\w+)\.com";
string input = "user@example.com";

Match match = Regex.Match(input, pattern);
if (match.Success) {
    string fullMatch = match.Groups[0].Value; // entire match
    string user = match.Groups[1].Value;      // first group
    string domain = match.Groups[2].Value;    // second group
}
💻

Example

This example shows how to extract the username and domain from an email address using regex groups in C#.

csharp
using System;
using System.Text.RegularExpressions;

class Program {
    static void Main() {
        string pattern = "(\\w+)@(\\w+)\\.com";
        string input = "contact@openai.com";

        Match match = Regex.Match(input, pattern);
        if (match.Success) {
            Console.WriteLine($"Full match: {match.Groups[0].Value}");
            Console.WriteLine($"Username: {match.Groups[1].Value}");
            Console.WriteLine($"Domain: {match.Groups[2].Value}");
        } else {
            Console.WriteLine("No match found.");
        }
    }
}
Output
Full match: contact@openai.com Username: contact Domain: openai
⚠️

Common Pitfalls

Common mistakes when using regex groups in C# include:

  • Forgetting to escape special characters like \. in the pattern.
  • Accessing group indexes that do not exist, causing runtime errors.
  • Using non-capturing groups (?:...) when you want to capture data.
  • Assuming group 0 is a captured group instead of the full match.

Always check Match.Success before accessing groups to avoid errors.

csharp
using System;
using System.Text.RegularExpressions;

class Program {
    static void Main() {
        string pattern = "(\\w+)@(\\w+)\.com"; // Correctly escaped dot
        string input = "user@example.com";

        Match match = Regex.Match(input, pattern);
        if (match.Success) {
            // This will work as expected because dot is escaped
            Console.WriteLine(match.Groups[2].Value);
        }
    }
}
📊

Quick Reference

  • Group 0: Entire matched string.
  • Group 1, 2, ...: Captured groups in order.
  • Regex.Match(input, pattern): Finds first match.
  • Match.Groups[index].Value: Gets group text.
  • Escape special characters like \. in patterns.

Key Takeaways

Use parentheses () in your regex pattern to create capturing groups.
Access captured groups with Match.Groups[index], where 0 is the full match.
Always check Match.Success before reading groups to avoid errors.
Escape special characters like dot (.) with a backslash (\.) in patterns.
Non-capturing groups (?:...) do not store matched text for extraction.