0
0
CsharpHow-ToBeginner · 4 min read

How to Use Regex.Match in C# for Pattern Matching

Use Regex.Match in C# to search a string for the first occurrence of a pattern defined by a regular expression. It returns a Match object that contains information about the match, including success status and matched text.
📐

Syntax

The Regex.Match method has this basic syntax:

  • Regex.Match(string input, string pattern): Searches the input string for the first match of the pattern.
  • input: The text to search.
  • pattern: The regular expression pattern to find.
  • Returns a Match object with details about the match.
csharp
Match match = Regex.Match(input, pattern);
💻

Example

This example shows how to find the first word starting with a capital letter in a sentence using Regex.Match. It prints the matched word if found.

csharp
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string input = "Hello world from CSharp.";
        string pattern = "\\b[A-Z][a-z]*\\b"; // Word starting with capital letter

        Match match = Regex.Match(input, pattern);

        if (match.Success)
        {
            Console.WriteLine("Found match: " + match.Value);
        }
        else
        {
            Console.WriteLine("No match found.");
        }
    }
}
Output
Found match: Hello
⚠️

Common Pitfalls

Common mistakes when using Regex.Match include:

  • Not escaping special characters in the pattern, which can cause errors or unexpected matches.
  • Assuming Regex.Match finds all matches; it only finds the first. Use Regex.Matches for multiple matches.
  • Not checking Match.Success before accessing Match.Value, which can cause runtime errors if no match is found.
csharp
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string input = "Price is $5.";
        string pattern = "\$\d+"; // Correctly escaped $ to match literal dollar sign

        // Match with correctly escaped pattern
        Match match = Regex.Match(input, pattern);
        Console.WriteLine("Match success: " + match.Success);
        if (match.Success)
        {
            Console.WriteLine("Matched value: " + match.Value);
        }
    }
}
Output
Match success: True Matched value: $5
📊

Quick Reference

Remember these tips when using Regex.Match:

  • Use Regex.Match(input, pattern) to find the first match.
  • Check Match.Success before using Match.Value.
  • Escape special regex characters like \$, \., \* in your pattern.
  • For multiple matches, use Regex.Matches.

Key Takeaways

Use Regex.Match to find the first pattern match in a string and get detailed match info.
Always check Match.Success before accessing the matched text to avoid errors.
Escape special regex characters in your pattern to match them literally.
Regex.Match returns only the first match; use Regex.Matches for all matches.
Patterns are case-sensitive by default; use RegexOptions.IgnoreCase to ignore case.