In C#, regular expressions (regex) provide a powerful and flexible way to perform pattern matching and text manipulation. Regex allows you to search, validate, and extract information from strings based on specific patterns. In this article, we’ll explore the usage of regex in C#, its syntax, common patterns, and how to apply it effectively in your code.

Understanding Regex

A regular expression is a sequence of characters that defines a search pattern. It consists of literal characters and metacharacters that represent rules and conditions. In C#, you can use the Regex class from the System.Text.RegularExpressions namespace to work with regular expressions.

Basic Regex Usage

To use regex in C#, you typically follow these steps:

  1. Create a regex pattern using the desired metacharacters and literals.
  2. Compile the pattern into a Regex object.
  3. Use the Regex object to perform operations such as searching, matching, replacing, or extracting.

Here’s an example that demonstrates a basic usage of regex in C#:

using System;
using System.Text.RegularExpressions;
public class RegexExample
    public static void Main()
        string text = "Hello, my email address is";
        // Define a regex pattern to match email addresses
        string pattern = @"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b";
        // Compile the regex pattern
        Regex regex = new Regex(pattern);
        // Find matches in the text
        MatchCollection matches = regex.Matches(text);
        // Print all matches
        foreach (Match match in matches)

In this example, we define a regex pattern to match email addresses. We then compile the pattern into a Regex object and use it to find matches in the given text. The matches are stored in a MatchCollection, and we iterate over the matches to print them.

Common Regex Patterns

Regex patterns can vary depending on your specific needs, but here are some common patterns and metacharacters:

  • .: Matches any character except a newline.
  • *: Matches zero or more occurrences of the preceding element.
  • +: Matches one or more occurrences of the preceding element.
  • ?: Matches zero or one occurrence of the preceding element.
  • []: Defines a character class, matching any single character within the brackets.
  • ^: Matches the start of a line or string.
  • $: Matches the end of a line or string.
  • \b: Matches a word boundary.

These are just a few examples of the many metacharacters and patterns available in regex. Consult the documentation or online resources for a comprehensive list.

Regex Methods and Operations

The Regex class in C# provides various methods for regex operations, including:

  • Match: Searches for the first occurrence of the pattern in the input string.
  • Matches: Searches for all occurrences of the pattern in the input string.
  • Replace: Replaces matched patterns in the input string with a specified replacement string.
  • Split: Splits the input string into an array of substrings based on the pattern.

These methods offer flexibility and allow you to perform a wide range of regex operations for text manipulation and processing.


C# regex provides a powerful tool for pattern matching and text manipulation. By understanding regex syntax, common patterns, and utilizing the Regex class, you can effectively search, validate, and manipulate strings in your C# code. Regex enables you to perform complex matching operations and extract meaningful information from text, enhancing the functionality and versatility of your applications.

Click Here to read more C# / ASP .NET tutorials.

Categorized in: