0
0
CsharpHow-ToBeginner · 3 min read

How to Use Regex in C#: Simple Guide with Examples

In C#, use the System.Text.RegularExpressions.Regex class to work with regular expressions. Create a Regex object with a pattern string, then use methods like IsMatch or Match to find or check text patterns.
📐

Syntax

The basic syntax to use regex in C# involves creating a Regex object with a pattern string. You can then call methods like IsMatch to check if a string matches the pattern, or Match to get the matching part.

  • Regex regex = new Regex(pattern); - creates a regex object with your pattern.
  • regex.IsMatch(input); - returns true if input matches the pattern.
  • regex.Match(input); - returns the first match found in input.
csharp
using System.Text.RegularExpressions;

string pattern = "\\d+"; // matches one or more digits
Regex regex = new Regex(pattern);

string input = "Order number 12345";
bool isMatch = regex.IsMatch(input);
Match match = regex.Match(input);
💻

Example

This example shows how to check if a string contains a number and extract it using regex in C#.

csharp
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string pattern = "\\d+"; // pattern to find digits
        Regex regex = new Regex(pattern);

        string input = "Invoice ID: 7890";

        if (regex.IsMatch(input))
        {
            Match match = regex.Match(input);
            Console.WriteLine($"Found number: {match.Value}");
        }
        else
        {
            Console.WriteLine("No number found.");
        }
    }
}
Output
Found number: 7890
⚠️

Common Pitfalls

Common mistakes when using regex in C# include:

  • Not escaping special characters properly in the pattern string (use double backslashes \\ in C# strings).
  • Using Regex.Match without checking if a match exists, which can cause errors.
  • Ignoring case sensitivity when needed (use RegexOptions.IgnoreCase).

Always test your regex patterns and handle cases where no match is found.

csharp
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string pattern = "\\d+"; // correct escaping
        Regex regex = new Regex(pattern);

        string input = "No digits here";

        // Wrong: assuming match always exists
        // Console.WriteLine(regex.Match(input).Value); // This prints empty string

        // Right: check if match found
        Match match = regex.Match(input);
        if (match.Success)
        {
            Console.WriteLine($"Found: {match.Value}");
        }
        else
        {
            Console.WriteLine("No match found.");
        }
    }
}
Output
No match found.
📊

Quick Reference

Here are some quick tips for using regex in C#:

  • Use Regex.IsMatch(string) to check if text matches a pattern.
  • Use Regex.Match(string) to get the first match.
  • Use Regex.Matches(string) to get all matches.
  • Escape special characters in patterns with double backslashes \\.
  • Use RegexOptions.IgnoreCase to ignore case.

Key Takeaways

Use System.Text.RegularExpressions.Regex class to work with regex in C#.
Escape special characters in patterns with double backslashes.
Check if a match exists before accessing match results.
Use RegexOptions to customize matching behavior like ignoring case.
Regex methods include IsMatch, Match, and Matches for different needs.