top of page

C# Regex: A Complete Guide

Updated: Feb 21

Regex, short for Regular Expressions, is a tool used in computer science and programming for pattern matching within strings. It provides a concise and flexible means for matching, searching, and replacing text based on specific patterns.


The importance of regular expressions lies in their versatility and efficiency in handling complex string manipulation tasks. They allow developers to define patterns using a combination of literal characters and special metacharacters, providing a way to express intricate search criteria succinctly.


C# Regex:A complete guide

C# Regex class is part of the .NET framework and provides methods and properties for working with regular expressions. It offers functionalities for pattern matching, replacement, and extraction of text based on user-defined regular expressions. By using the C# Regex class in C#, developers can perform sophisticated text processing tasks with ease and efficiency.


In the following sections, we'll explore the practical aspects of utilizing C#'s Regex class. We'll cover various elements such as Regex methods, quantifiers, lookahead, lookbehind, as well as groups and capturing within C# Regex. Moreover, we'll provide examples showcasing the application of C# Regex in real-world scenarios to illustrate its effectiveness and versatility.


In case you missed:


Table of Contents:


C# Regex Syntax

The C# Regex syntax follows a general pattern, comprising various elements. Below is a structured formula representing the basic structure of a C# regex:

/pattern/modifiers

Pattern: This is the regular expression pattern itself, enclosed within forward slashes (/). It defines the sequence of characters or metacharacters that the regex engine should match against the input string.


Modifiers: These are optional flags that modify the behavior of the regex pattern. They are appended after the closing slash. Common modifiers include:

  • i: Ignore case. Matches are case-insensitive.

  • m: Treats the input as multiple lines. ^ and $ anchors match the start and end of each line, not just the start and end of the entire input string.

  • s: Treats the input as a single line. The dot (.) matches any character, including newline characters.

  • x: Ignore pattern whitespace. Allows you to include comments and whitespace within the pattern for better readability.


Here's an example of a complete regex pattern with modifiers:

/\d{3}-\d{3}-\d{4}/i

This regex pattern matches a phone number in the format ###-###-####, and the i modifier makes the match case-insensitive.


Basic C# Regex Syntax Elements

Below are some basic C# regex syntax elements:


Literals: Match the literal characters themselves.

  • Example: "hello" matches the string "hello" exactly.


Metacharacters: Characters with special meanings in regex.

  • .: Matches any single character except newline.

  • ^: Matches the start of a line.

  • $: Matches the end of a line.

  • *: Matches zero or more occurrences of the preceding character.

  • +: Matches one or more occurrences of the preceding character.

  • ?: Matches zero or one occurrence of the preceding character.

  • []: Matches any single character within the brackets.

  • |: Acts as an OR operator, matches either the expression before or after the pipe.

  • (): Groups expressions together.


Character Classes:

  • \d: Matches a digit (0-9).

  • \D: Matches a non-digit.

  • \w: Matches a word character (alphanumeric and underscore).

  • \W: Matches a non-word character.

  • \s: Matches whitespace (spaces, tabs, newlines).

  • \S: Matches non-whitespace.


Quantifiers:

  • {n}: Matches exactly n occurrences of the preceding character.

  • {n,}: Matches at least n occurrences of the preceding character.

  • {n,m}: Matches between n and m occurrences of the preceding character.


Anchors:

  • ^: Anchors the regex to the start of a line.

  • $: Anchors the regex to the end of a line.


Escape Sequences: To match metacharacters literally, you need to escape them with a backslash (\). For example, to match a literal dot, you use \..


Getting Started with C# Regex

To use the Regex class in C#, you first need to import the System.Text.RegularExpressions namespace. Then, you can create an instance of the Regex class, passing the regular expression pattern as a string to the constructor.


The System.Text.RegularExpressions namespace contains classes that provide access to .NET’s regular expression engine. The primary class in this namespace is the Regex class.


Before you can use the Regex class (or any other class in the System.Text.RegularExpressions namespace), you need to import the namespace into your C# file. This is done with the using keyword at the top of your file, like so:

using System.Text.RegularExpressions;

Once you’ve imported the namespace, you can create an instance of the Regex class. This is done by calling the Regex constructor and passing in a string that represents your regular expression pattern.


Here’s an example:

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

In this example, \d+ is a regular expression that matches one or more digits. The new Regex(pattern); line creates a new instance of the Regex class, using the pattern you specified.


An example of Matching Patterns

Let’s say you have a string and you want to check if it contains any numbers. You can use the Regex.IsMatch method, which is a method provided by the Regex class in C#. It checks if a specific pattern (regular expression) matches a given string.


Here's how you can use it:


Consider the below example that demonstrates the usage of regular expression (Regex) to check if a given input string contains any numbers.

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        Console.WriteLine("Enter a string:");
        string input = Console.ReadLine();
        
        string pattern = @"\d+"; // matches one or more digits
        Regex regex = new Regex(pattern);

        bool containsNumber = regex.IsMatch(input);
        Console.WriteLine($"Does the input string contain any numbers? {containsNumber}");
    }
}

C# Regex: Basic example


C# Regex Methods


Match():

The Match() method searches an input string for a substring that matches a regular expression pattern and returns the first occurrence as a single Match object. If no match is found, the Success property of the returned Match object is false.


Here’s an example that demonstrates the usage of C# Regex to find the first occurrence of one or more digits in a given input string and print information about the match:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string input = "Hello, World 123!";
        string pattern = @"\d+"; // matches one or more digits
        Regex regex = new Regex(pattern);
        Match match = regex.Match(input);

        if (match.Success)
        {
            Console.WriteLine($"Match found at index {match.Index} with value {match.Value}.");
        }
    }
}

The Match method of the Regex class is used to search for the first occurrence of the pattern in the input string. The resulting Match object is stored in the variable match.


It checks if the match was successful using the Success property of the Match object.


If the match is successful, it prints the index where the match was found and the value of the matched substring.


C# Regex: Match() method

In this case, it prints "Match found at index 13 with value 123.", indicating that the first occurrence of one or more digits was found starting at index 13 in the input string.


Matches():

The Matches() method searches an input string for all occurrences of a regular expression and returns all the matches as a MatchCollection. Each item in the MatchCollection represents one match and can be accessed like an array.


Here's an example that demonstrates how to extract email addresses from a given input string using regular expressions in C#:

using System; 
using System.Text.RegularExpressions; 

class Program 
{ 
	static void Main() 
	{ 
		string input = "Contact us at email@thetechplatform.com or support@thetechplatform.org for assistance."; 
		string pattern = @"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b"; 

		// matches email addresses 
		Regex regex = new Regex(pattern); 
		MatchCollection matches = regex.Matches(input); 

		foreach (Match match in matches) 
		{ 
			Console.WriteLine($"Email address found: {match.Value}"); 
		} 
	} 
}

C# Regex: Matches() method

The Matches method of the Regex class is used to find all occurrences of the pattern in the input string. The resulting MatchCollection object is stored in the variable matches.


It iterates through each Match object in the matches collection using a foreach loop.


For each match, it prints the email address found. In this case, it prints two lines, each containing an extracted email address from the input string.


Here's another example that demonstrates how to extract phone numbers from a given input string using regular expressions in C#:

using System; 
using System.Text.RegularExpressions; 

class Program 
{ 
	static void Main() 
	{ 
		string input = "Contact us at +1 (123) 456-7890 or 555-5555 for assistance."; 
		string pattern = @"\b(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})\b"; 
		// matches phone numbers 
		
		Regex regex = new Regex(pattern); 
		MatchCollection matches = regex.Matches(input); 

		foreach (Match match in matches) 
		{ 
			string phoneNumber = match.Groups[0].Value; 
			// Get the whole matched phone number 
			
			Console.WriteLine($"Phone number found: {phoneNumber}"); 
		} 
	} 
}

C# Regex: Matches() method 2


Replace():

The Replace() method replaces all strings in an input string that match a regular expression pattern with a specified replacement string.


Here’s an example that demonstrates how to perform simple string replacement using regular expressions in C#. It's a common task in text processing when you need to find specific patterns and replace them with desired strings.

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string input = "The quick brown fox jumps over the lazy dog.";
        string pattern = "fox";
        string replacement = "cat";
        Regex regex = new Regex(pattern);
        string result = regex.Replace(input, replacement);

        Console.WriteLine("Original string:");
        Console.WriteLine(input);

        Console.WriteLine("\nString after replacement:");
        Console.WriteLine(result);
    }
}

C# Regex: Replace() Method


Split():

The Split() method splits an input string into an array of substrings at the positions defined by a regular expression match.


Here’s an example:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string input = "apple banana-orange:grape,pear";
        string pattern = @"\s|-|:|,"; // matches a space, dash, colon, or comma
        Regex regex = new Regex(pattern);
        string[] result = regex.Split(input);

        foreach (string str in result)
        {
            Console.WriteLine(str);
        }
    }
}

In this example, the Split() method splits the input string at each space, dash, colon, or comma.


C# Regex:Split() method

Quantifiers in C# Regex

Quantifiers are metacharacters in regular expressions that specify the quantity of the preceding element. They define a specified number of times a particular element should occur in an input string when matching.


Here are the common quantifiers in C# Regex:

  1. * (Asterisk): Matches the preceding element zero or more times.

  2. + (Plus): Matches the preceding element one or more times.

  3. ? (Question Mark): Matches the preceding element zero or one time.

  4. {n} (Curly Braces): Matches the preceding element exactly n times.

  5. {n,} (Curly Braces): Matches the preceding element at least n times.

  6. {n,m} (Curly Braces): Matches the preceding element from n to m times.


Quantifiers can be greedy or lazy.

  1. Greedy quantifiers match as many occurrences of particular patterns as possible

  2. Lazy quantifiers match as few occurrences as possible. Appending the ? character to a quantifier makes it lazy.


For example, the regular expression pattern a* will match as many ‘a’ characters as possible, while a*? will match as few ‘a’ characters as possible


Here are some examples of using quantifiers in C# Regex:


Example 1: Match Zero or More Times (*):

The * quantifier matches the preceding element zero or more times. It’s equivalent to the {0,} quantifier.


Let's consider an example where we want to extract all words that start with the letter 'A' from a given input string:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string input = "Apple is a fruit. Ape is an animal. Bananas are also fruits.";
        string pattern = @"\bA\w*\b"; // matches words starting with 'A'
        Regex regex = new Regex(pattern);
        MatchCollection matches = regex.Matches(input);

        foreach (Match match in matches)
        {
            Console.WriteLine($"Word starting with 'A': {match.Value}");
        }
    }
}

C# Regex: Quantifier example 1

This example demonstrates how to use regular expressions in C# to find words that start with the letter 'A' in a given text. It defines a regular expression pattern \bA\w*\b, which matches words starting with 'A'.


Example 2: Match One or More Times (+):

The + quantifier matches the preceding element one or more times.


This code will extract all sequences of digits (numbers) from the input string "C# 12 and .NET 8" and print them.

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string pattern = @"\d+";
        string input = "C# 12 and .NET 8";
        
        foreach (Match match in Regex.Matches(input, pattern))
        {
            Console.WriteLine(match.Value);
        }
    }
}

C# Regex:Quantifier example 2

It uses Regex.Matches method to find all occurrences of the pattern in the input string.


Inside the foreach loop, for each match found, it prints the matched value (match.Value).


Groups and Capturing in C# Regex


Groups:

In regular expressions, parentheses () are used to define groups. Groups allow you to treat multiple characters as a single unit, apply quantifiers to multiple characters, and isolate parts of a pattern so that you can apply a regex operator to the entire group.


There are several types of group mechanisms in C# Regex, which are as follows:

  1. Capturing Groups: These are the most common type of groups, denoted by parentheses (). They match the pattern inside the parentheses and capture the matched substring for use after the match is found.

  2. Non-Capturing Groups: Denoted by (?:), non-capturing groups match the pattern but do not capture the result. They’re useful when you need to group part of a pattern, but don’t need to reuse the matched substring.

  3. Named Groups: Named groups are capturing groups with an additional identifier. Instead of referring to them by their position in the pattern, you can refer to them by a chosen name. They’re denoted by (?<name>).

  4. Balancing Groups: Balancing groups are a feature of .NET regular expressions that allow you to match balanced pairs of delimiters, such as parentheses or brackets.


Capturing:

By default, every group you create in a regular expression is a capturing group. Capturing groups are automatically numbered from left to right based on the order of the opening parentheses in the regular expression, starting from. The group at index 0 represents the text matched by the entire regular expression pattern.


In C# Regex supports several types of capturing mechanisms:

  1. Numbered Capturing: By default, capturing groups are numbered automatically from left to right based on the order of the opening parentheses in the regular expression, starting from 1. The group at index 0 represents the text matched by the entire regular expression pattern.

  2. Named Capturing: Named capturing allows you to access captured groups by a chosen name rather than by numerical index. This can make your code easier to read and maintain, especially if your regular expression contains many groups


Let's consider a specific example where we want to extract information from a string containing product codes. Suppose the product codes follow a specific format: <Category>-<Subcategory>-<ID>. We want to extract each component (category, subcategory, and ID) separately.


Here's a code example:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string input = "Electronics-Phones-12345";
        string pattern = @"(?<Category>[^-]+)-(?<Subcategory>[^-]+)-(?<ID>[^-]+)";
        Regex regex = new Regex(pattern);
        Match match = regex.Match(input);

        if (match.Success)
        {
            string category = match.Groups["Category"].Value;
            string subcategory = match.Groups["Subcategory"].Value;
            string id = match.Groups["ID"].Value;

            Console.WriteLine($"Category: {category}");
            Console.WriteLine($"Subcategory: {subcategory}");
            Console.WriteLine($"ID: {id}");
        }
    }
}

C# Regex: Group and capturing


Lookaheads and Lookbehinds in C# Regex


Lookaheads:

Lookaheads are a type of assertion in regular expressions that allow you to match a pattern only if it is followed by another pattern. The syntax for a lookahead is A(?=B), where A is the pattern you want to match and B is the pattern that must follow A.


There are two types of Lookaheads in C# Regex:

  1. Positive Lookahead (?=...): Asserts that what immediately follows the current position in the string matches the specified pattern.

  2. Negative Lookahead (?!...): Asserts that what immediately follows the current position in the string does not match the specified pattern.


Here’s an example of a lookahead in C# to match a number followed by the word "dollars" in a given input string.

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string input = "100 dollars";
        string pattern = @"\d+(?=\s*dollars)";
        Regex regex = new Regex(pattern);
        Match match = regex.Match(input);

        if (match.Success)
        {
            Console.WriteLine($"Match found: {match.Value}");  // Outputs: "100"
        }
    }
}

In this example, the pattern \d+(?=\s*dollars) matches one or more digits (\d+) only if they are followed by zero or more spaces (\s*) and the word “dollars”.


Lookbehinds:

Lookbehinds are similar to lookaheads, but they match a pattern only if it is preceded by another pattern. The syntax for a lookbehind is (?<=B)A, where A is the pattern you want to match and B is the pattern that must precede A.


There are two types of Lookbehinds in C# Regex:

  1. Positive Lookbehind (?<=...): Asserts that what immediately precedes the current position in the string matches the specified pattern.

  2. Negative Lookbehind (?<!...): Asserts that what immediately precedes the current position in the string does not match the specified pattern.


Let's consider an example where we want to extract the username from an email address. We'll use a regular expression to match the username part before the "@" symbol in the email address.

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string input = "support@thetechplatform.com";
        string pattern = @"(?<=^)[^@]+";
        Regex regex = new Regex(pattern);
        Match match = regex.Match(input);

        if (match.Success)
        {
            Console.WriteLine($"Username: {match.Value}");  // Outputs: "john.doe"
        }
    }
}

The input string "support@thetechplatform.com" represents an email address. The regular expression pattern @"(?<=^)[^@]+" uses a positive lookbehind (?<=^) to match one or more characters [^@]+ that are not "@" character, at the start of the string.


C# Regex: Lookbehind

These lookahead and lookbehind assertions in C# Regex are zero-width, meaning they do not consume characters in the string, but only assert whether a match is possible or not.


C# Regex Options

In C#, the RegexOptions enumeration provides several options to modify the behavior of regular expressions. Here are some of them:

  1. IgnoreCase

  2. Multiline

  3. Singleline


IgnoreCase (RegexOptions.IgnoreCase):

Specifies case-insensitive matching. For example, the pattern “abc” will match “abc”, “Abc”, “aBc”, “abC”, “ABc”, “AbC”, “aBC”, and “ABC”.

class Program
{
    static void Main()
    {
        string input = "Hello, World!";
        string pattern = "HELLO";
        Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
        bool isMatch = regex.IsMatch(input);

        Console.WriteLine(isMatch);  // Outputs: True
    }

In this example, even though the pattern “HELLO” is in uppercase and the input string is in mixed case, IsMatch returns true because we’re using RegexOptions.IgnoreCase.


Multiline (RegexOptions.Multiline):

Changes the meaning of ^ and $ so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string.

class Program
{
    static void Main()
    {
        string input = "Hello,\nWorld!";
        string pattern = "^World";
        Regex regex = new Regex(pattern, RegexOptions.Multiline);
        bool isMatch = regex.IsMatch(input);

        Console.WriteLine(isMatch);  // Outputs: True
    }

In this example, IsMatch returns true because we’re using RegexOptions.Multiline, which makes the ^ anchor match the start of each line, not just the start of the input string.


Singleline (RegexOptions.Singleline):

Changes the meaning of the dot (.) so it matches every character (instead of every character except \n).

class Program
{
    static void Main()
    {
        string input = "Hello,\nWorld!";
        string pattern = "Hello,.World";
        Regex regex = new Regex(pattern, RegexOptions.Singleline);
        bool isMatch = regex.IsMatch(input);

        Console.WriteLine(isMatch);  // Outputs: True
    }

In this example, IsMatch returns true because we’re using RegexOptions.Singleline, which makes the. character match any character, including newline characters.


Common Use Cases of C# Regex

C# Regex finds various applications across different domains. Some common use cases include:

  1. Validation: Regular expressions are commonly used for input validation. They ensure that user input conforms to specific patterns or formats, such as email addresses, phone numbers, passwords, and more.

  2. Data Extraction: C# Regex is used to extract specific information from strings or documents. For instance, extracting URLs, email addresses, hashtags, or mentions from social media posts or web pages.

  3. Text Manipulation: Regular expressions enable text manipulation tasks like replacing, splitting, or formatting strings based on certain patterns. For example, replacing specific words or characters, splitting text into tokens, or formatting phone numbers or dates.

  4. Search and Filtering: C# Regex allows searching for specific patterns within a larger text or document. It's useful for filtering data based on certain criteria, such as searching for log entries containing errors, filtering out specific keywords, or finding lines matching a specific format.

  5. Syntax Highlighting and Parsing: In code editors or IDEs, C# regex is used for syntax highlighting, parsing, and code analysis. It helps identify and highlight code elements like keywords, strings, comments, and identifiers based on predefined patterns.

  6. Web Scraping: C# Regex plays a crucial role in web scraping tasks, where it's used to extract structured data from HTML or XML documents. It helps locate and extract information from specific HTML elements or attributes.

  7. Data Transformation: Regular expressions assist in transforming data from one format to another. For example, converting plain text into HTML markup, transforming data between different data formats (e.g., CSV to JSON), or reformatting data for import/export operations.

  8. Log Parsing: Regex is commonly employed for parsing log files to extract relevant information like timestamps, IP addresses, error messages, and more. It helps in analyzing and troubleshooting system or application logs.

  9. String Matching and Pattern Recognition: Regular expressions enable efficient string matching and pattern recognition tasks. It's used in natural language processing, sentiment analysis, text mining, and other text analysis tasks to identify patterns or features within text data.

  10. Data Validation in Forms and Applications: In web forms or desktop applications, C# regex is used for client-side and server-side validation of user input. It ensures that data entered by users meets specific criteria, preventing invalid or malicious input.

Let’s go through some common use case examples of C# Regex:


Validating User Input:

Regular expressions are often used to validate user input, such as email addresses, phone numbers, and passwords.

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        // Email validation pattern
        string emailPattern = @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
        Regex emailRegex = new Regex(emailPattern);

        // Prompt user to input email addresses
        Console.WriteLine("Enter an email address:");
        string userEmail1 = Console.ReadLine();
        Console.WriteLine("Enter another email address:");
        string userEmail2 = Console.ReadLine();

        // Validate the first email address
        bool isValidEmail1 = emailRegex.IsMatch(userEmail1);

        // Validate the second email address
        bool isValidEmail2 = emailRegex.IsMatch(userEmail2);

        // Output validation results
        Console.WriteLine($"Is '{userEmail1}' a valid email address? {isValidEmail1}");
        Console.WriteLine($"Is '{userEmail2}' a valid email address? {isValidEmail2}");
    }
}

Searching Within Text:

You can use regular expressions to search for patterns within text. For example, you can find all words in a string that start with a capital letter.

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        // Prompt user to input text
        Console.WriteLine("Enter some text:");
        string text = Console.ReadLine();

        // Regular expression pattern to find words starting with a capital letter
        string pattern = @"\b[A-Z]\w*\b";

        // Iterate through matches and print words starting with a capital letter
        foreach (Match match in Regex.Matches(text, pattern))
        {
            Console.WriteLine(match.Value);
        }
    }
}


Conclusion

Understanding C# Regex and how to use them effectively can greatly enhance your ability to work with text data. From simple validation tasks to complex text processing workflows, regex offers a versatile and powerful toolset. By mastering the concepts and techniques covered in this guide, you'll be well-equipped to tackle a wide range of text processing challenges in your C# projects.

Comments


bottom of page