C# Regex: Introduction to Regular Expressions in C# with Examples

Regular Expressions (Regex) are a powerful tool for text processing that allows you to search, replace, and manipulate text based on patterns.
In C#, Regex is implemented through the Regex class, which provides a set of methods for working with regular expressions.

This article will explore the basics of Regular Expressions in C# and provide examples of creating Regex patterns using Regex methods. We will also discuss the benefits of using Regular Expressions in C# and some advanced techniques for working with Regex.

c-sharp-regex-examples
C# Regex

Definition of Regular Expressions

A regular expression is a pattern that is used to match the text. Regular expressions are used in programming languages, text editors, and other applications to search, validate, and manipulate text. Regular expressions are built using metacharacters and literal characters.

C# Regex Class

The C# Regex class provides a set of methods for working with regular expressions. The Regex class is available in System.Text.RegularExpressions namespace. Some of the most commonly used methods are:

  • Regex.IsMatch: This method determines whether a string matches a specified regular expression pattern.
  • Regex.Match: Searches a string for the first occurrence of a specified regular expression pattern.
  • Regex.Matches: Searches a string for all occurrences of a specified regular expression pattern.
  • Regex.Replace: It replaces all occurrences of a specified regular expression pattern with a specified replacement string.
  • Regex.Split: It splits a string into an array of substrings based on a specified regular expression pattern.

Example: C# Regex

Let’s look at an example of how to use the Regex class in C#:

Suppose we want to match a string that contains one or more digits. We can use the “\d+” pattern, where “\d” matches a digit character, and “+” matches one or more occurrences of the preceding pattern.

using System;
using System.Text.RegularExpressions;

class RegexExample
{
    static void Main(string[] args)
    {
        string input = "abc123def456ghi";
        MatchCollection matches = Regex.Matches(input, @"\d+");

        foreach (Match match in matches)
        {
            Console.WriteLine(match.Value);
        }

    }
}
csharp regex example

Output:

123
456

Using metacharacters to define patterns

Metacharacters are characters that have a special meaning in a regular expression. Here are some of the most commonly used metacharacters in regular expressions:

PatternDescription
. (dot)matches any single character except newline.
*matches zero or more occurrences of the preceding character or group.
+matches one or more occurrences of the preceding character or group.
?matches zero or one occurrence of the preceding character or group.
^matches the beginning of the line or string.
$matches the end of the line or string.
[]matches any single character in the specified set of characters.
[^]matches any single character, not in the specified set of characters.
|matches either the expression before or after the vertical bar.

Example 2: Matching a string with zero or more occurrences of a pattern

Suppose we want to match a string that contains zero or more occurrences of the word “hello”. We can use the “hello” pattern, where “” matches zero or more occurrences of the preceding pattern.

string input = "hello world, hello universe";
MatchCollection matches = Regex.Matches(input, @"hello*");

foreach (Match match in matches)
{
    Console.WriteLine(match.Value);
}

Output:

hello
hello

Example 3: Matching a string with a specific number of occurrences of a pattern

Suppose we want to match a string that contains exactly three digits. We can use the “\d{3}” pattern, where “{3}” matches exactly three occurrences of the preceding pattern.

string input = "abc123def456ghi789";
MatchCollection matches = Regex.Matches(input, @"\d{3}");

foreach (Match match in matches)
{
    Console.WriteLine(match.Value);
}

Output:

123
456
789

How Regex Works?

A regular expression is a pattern that is used to match the text. Regular expressions are built using metacharacters, characters with a special meaning in a regular expression. For example, the dot (.) metacharacter matches any character, while the asterisk (*) metacharacter matches zero or more occurrences of the previous character.

Regular expressions work by comparing a pattern to a string of text. If the pattern matches the text, then the regular expression is said to have found a match. Regular expressions can be used to search for patterns in text, validate user input, and manipulate text strings.

Benefits of using Regular Expressions in C#

There are many benefits to using regular expressions in C#. Regular expressions allow you to:

  • Validate user input: Regular expressions can be used to ensure user input is correct. For example, you can use a regular expression to validate an email address, phone number, or zip code.
  • Search and replace text: Regular expressions can be used to search and replace text in a string. For example, you can use a regular expression to replace all occurrences of a word in a string.
  • Extract data from the text: Regular expressions can be used to extract data from a string. For example, you can use a regular expression to extract all phone numbers from a block of text.
  • Manipulate text strings: Regular expressions can be used to manipulate text strings. For example, you can use a regular expression to remove all whitespace from a string.

Working with Regex Methods in C#

C# Regex provides several methods for working with regular expressions. The most commonly used methods are Match, Replace, and Split.

01. Using the Match Method to Find Matches in a String

The Match method is used to find the first occurrence of a regular expression pattern in a string. It returns a Match object that contains information about the match.

Example : Using Match to Find a Single Match in a String

Suppose we want to find the first occurrence of the word “apple” in a string. We can use the following code:

using System;
using System.Text.RegularExpressions;
class RegexExample
{
    static void Main(string[] args)
    {
        string input = "I like apples.";
        string pattern = "apple";
        Match match = Regex.Match(input, pattern);
        if (match.Success)
        {
            Console.WriteLine("Match found: " + match.Value);
        }
    }
}

Output:

Match found: apple

Example: Using Grouping to Extract Specific Parts of a Matched String

Grouping in regular expressions allows you to match a pattern and then extract a specific part of the matched string.

You can use parentheses to define a group and then refer to that group in your code to extract the specific part of the string that was matched.

For example, let’s say you have a string that contains a date in the format of “MM/DD/YYYY“, and you want to extract just the year. You can use grouping to match the entire date pattern and then extract just the year portion by referring to the group in your code.

Here is an example of how to do this in C#:

using System;
using System.Text.RegularExpressions;
class RegexExample
{
    static void Main(string[] args)
    {
        string input = "Today's date is 03/10/2023";
        string pattern = @"\d{2}/\d{2}/(\d{4})"; // Group the year portion of the date

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

        if (match.Success)
        {
            string year = match.Groups[1].Value; // Extract the year portion from the group
            Console.WriteLine("The year is: " + year); // Output: The year is: 2023
        }

    }
}

02. Using the Replace method to replace matches in a string

The Replace method is used to replace one or more occurrences of a pattern in a string with a specified replacement string.

Example: Using C# Regex Replace method to replace a single match in a string

        string input = "The quick brown fox jumps over the lazy dog.";
        string pattern = "fox";
        string replacement = "cat";
        string result = Regex.Replace(input, pattern, replacement);
        Console.WriteLine(result);

Output:

The quick brown cat jumps over the lazy dog.

In this example, we replace the first occurrence of the pattern fox in the input string with the replacement string cat using the Replace method. The resulting string is then displayed in the console.

Example: Using Replace method to replace multiple matches in a string

        string input = "The quick brown fox jumps over the lazy dog.";
        string pattern = @"\b\w{5}\b";
        string replacement = "*****";
        string result = Regex.Replace(input, pattern, replacement);
        Console.WriteLine(result);

Output:

The ***** ***** fox ***** over the lazy dog.

In this example, we replace all occurrences of words that are exactly five characters long with the replacement string “*” using the Replace method. The resulting string is then displayed in the console.

03. C# Regex Split method

The Split method is a common string manipulation method that allows you to split a string into an array of substrings based on a specified separator. It can be very useful when working with strings and parsing data.

In C#, We can use the Split method with a regular expression pattern to split a string based on a specific pattern. It can be done using the Regex class available in the System.Text.RegularExpressions namespace.

The basic syntax for using the Regex Split method in C# is as follows:

string[] result = Regex.Split(input, pattern);

Here the input is the string you want to split, the pattern is the regular expression pattern you want to use to split the string, and the result is the array of substrings that the string was split into.

For example, let’s say you have the following string:

string input = "Hello, World!";

And you want to split it into an array of strings, with each array element containing one word. You could do this using the following regular expression pattern:

string pattern = @"\W+";

The following is code to split the string based on this regex pattern:

Example: C# Regex Split

using System;
using System.Text.RegularExpressions;
class RegexExample
{
    static void Main(string[] args)
    {
        string input = "Hello, World!";
        string pattern = @"\W+";
        string[] result = Regex.Split(input, pattern);
        Console.WriteLine($"Result: {result[0]} {result[1]}");

        // Result: Hello World

    }
}

The resulting array would contain two elements, “Hello” and “World”.

How to write a regular expression to validate an email address?

The following is the complete C# code example that uses a regular expression to validate an email address:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
        // Prompt the user for an email address
        Console.Write("Enter an email address: ");
        string email = Console.ReadLine();

        // Define a regular expression pattern for a simple email address
        string pattern = @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$";

        // Use the Regex.IsMatch method to check if the email address matches the pattern
        if (Regex.IsMatch(email, pattern))
        {
            Console.WriteLine("Valid email address!");
        }
        else
        {
            Console.WriteLine("Invalid email address.");
        }

        // Wait for the user to press a key before closing the console window
        Console.ReadKey();
    }
}

Output:

csharp-regex-example-to-validate-email-address
C# Regex

In this code example, we first prompt the user to enter an email address. We then define a regular expression pattern that matches a simple email address. This pattern matches the following format:

  • The email address must start with one or more alphanumeric characters, dots, underscores, percent signs, plus signs, or hyphens.
  • The email address must have @ symbol.
  • After the @ symbol, there must be one or more alphanumeric characters or dots, followed by a dot and two or more alphanumeric characters.

We then use the Regex.IsMatch method to check if the user’s input matches the pattern. If it does, we display a message that the email address is valid. We display a message saying the email address is invalid if it doesn’t.

Example: Replacing multiple white spaces using Regex in C#

Sometimes we may have a string with multiple white spaces, and we need to replace them with a single space or remove them altogether. We can achieve this using regular expressions in C#.

Here is an example of how to use Regex to replace multiple white spaces with a single space:

using System;
using System.Text.RegularExpressions;
class Program
{
    static void Main(string[] args)
    {
        string input = "This   is    a  string    with    multiple     spaces.";
        string pattern = @"\s+";
        string replacement = " ";

        string result = Regex.Replace(input, pattern, replacement);
        Console.WriteLine(result);
    }
}

Output:

This is a string with multiple spaces.

In the above code, we first declare a string variable input that contains the original string with multiple white spaces. We then define a regular expression pattern \s+ that matches one or more white spaces. Finally, we use the Regex.Replace method to replace all occurrences of the pattern with a single space and store the result in the result variable.

Example: Regex for Email Validation in C#

Email validation is an everyday use case for regular expressions. We can use a regular expression pattern to check whether an email address is valid. Here is an example of how to use Regex for email validation in C#:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
        string email = "example@email.com";
        string pattern = @"^[^@\s]+@[^@\s]+\.[^@\s]+$";

        bool isEmailValid = Regex.IsMatch(email, pattern);
        Console.WriteLine(isEmailValid);
    }
}
//Output: True

In the above code, we first declare a string variable email that contains the email address we want to validate. We then define a regular expression pattern ^[^@\s]+@[^@\s]+\.[^@\s]+$ that matches a valid email address.

Here’s a breakdown of the pattern:

  • ^: Matches the beginning of the string
  • [^@\s]+: Matches one or more characters that are not ‘@’ or whitespace
  • @: Matches the ‘@’ symbol
  • [^@\s]+: Matches one or more characters that are not ‘@’ or whitespace
  • \.: Matches a period character (escaped with a backslash)
  • [^@\s]+$: Matches one or more characters that are not ‘@’ or whitespace until the end of the string (‘$’ matches the end of the string)

Finally, we use the Regex.IsMatch method checks if the email string matches the pattern string and stores the result in the isEmailValid boolean variable.

FAQs

Q: What is a regular expression?

A regular expression is a pattern that can be used to match the text. It’s a powerful tool for string manipulation and searching.

Q: What namespace must you use to work with regular expressions in C#?

You need to use the System.Text.RegularExpressions namespace.

Q: What are some common regular expression operators?

Some common operators include:

The dot (.) which matches any single character.
The asterisk (*) which matches zero or more occurrences of the previous character.
The plus sign (+) which matches one or more occurrences of the previous character.
The question mark (?) which matches zero or one occurrence of the previous character.
Square brackets [] which match a single character that falls within the specified range or set of characters.
Parentheses () which can be used for grouping and capturing

Q: How do you check if a string matches a regular expression pattern in C#?

You can use the Regex.IsMatch method to check if a string matches a regular expression pattern. This method returns true if the string matches the pattern and false otherwise.

Q: How do you replace parts of a string using regular expressions in C#?

You can use the Regex.Replace method to replace parts of a string using regular expressions. This method takes a regular expression pattern and a replacement string and returns a new string with the matched parts replaced.

Q: Are regular expressions case-sensitive in C#?

By default, regular expressions are case-sensitive in C#. However, you can use RegexOptions.IgnoreCase option to make them case insensitive.

References: MSDN-Regex Class

You might also like:

Shekh Ali
4.5 2 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments