In this article, I will explain, what is Regular expression in C# and how we can use take benefit of using Regular Expressions (Regex) in C# by providing various C# Regex examples using Console application.

What is Regular Expression (Regex) in C#?

So, before we proceed to check various Regex examples, let's understand what is Regular expressions and how we can use them in C#.

Regular Expression is a pattern using which we can parse and confirm if a given text contains sequence of characters required by us (by using pattern) or not. To accomplish this, C# Regex Class offer methods and properties which we can use to parse the text. So, C# Regex or also known as C# Regular expression is sequence of characters that defines a pattern. A pattern may consist of literals, numbers, characters, operators, or constructs.

Namespace to use Regex or Regular expressions in C# is "System.Text.RegularExpressions".

Regex Class

Regex class is used to represent the regular expression in C#, so for example, if you want to check for zero of more white spaces, you can have regex as below

   // Create a pattern for zero or more whitespace sequence
   // * = watch 0 or more precedence
   // \s = to find any whitespace
    string pattern = @"\s*";  
    // Create a Regex  
    Regex rg = new Regex(pattern);  

Regex Class Methods

Regex class also contains few methods, which can be useful

  • IsMatch: Indicates whether the regular expression finds a match in the input string.
  • Matches: Searches an input string for all occurrences of a regular expression and returns all the matches.
  • Replace: replaces strings that match a regular expression pattern with a specified replacement string.
  • Split:Splits an input string into an array of substrings at the positions defined by a regular expression match.

Understanding Regex pattern using Cheatsheet

Let's understand basics for regex characters using the below table

Expression Meaning
. matches any character except newline
^ Word after this element matches the beginning of the string
$ Word before this element matches the end of the string
\w match any alphanumeric and underscore charact
\d matches any digit
\s matches any whitespace
\w\d\s matches word, digit, whitespace
\W\D\S not word, digit, whitespace
\n match a newline character
[] used to match the range of character
[abc] any of a, b, or c
[^a-z] any character not in range from a-z
\ used to match Escaped special character.
() used for group expression
(a|b) match either a or b

Regex Examples in C#

Let's try to make some examples using above details about Regular expressions.

So open your Visual Studio and Navigate to "File"-> "New" -> "Project" -> Select "Visual C#" from left-pane and "Console Application" from right-pane -> name your Project and click "Ok"

c-sharp-regex-examples-min.pngNow, let's create Regex examples one by one

Simple Regex Example in C#

Let's start with basic Regex, to find if given string is found within string or not.

using System;
using System.Text.RegularExpressions;

namespace RegexExamples
{
    public class Program
    {
        public static void Main(string[] args)
        {
			 string patternText = "Captain America";
            // Define a regular expression          
            Regex reg = new Regex(patternText);
 
            //IsMatch(string input)
            Console.WriteLine(reg.IsMatch("Captain America")); //'true'
			Console.WriteLine(reg.IsMatch("America")); // 'false' at it checks complete string
 
            //IsMatch(string input, int index)
            Console.WriteLine(reg.IsMatch("Captain America", 0));
 
        }
    }
}

Output:

True
False
True

In the above example, instead of defining any Regex patterns, we have used simple 'string' as a pattern, and then used "IsMatch" to check if string is found within strings.

Let's move on to move complex example, which will have Regex patterns.

Find duplicate words from a string using C# Regex

using System;
using System.Text.RegularExpressions;

namespace RegexExamples
{
    class Program
    {
        static void Main(string[] args)
        {
            // Define a regular expression for repeated words.
            Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b");

            // Define a test string.
            string text = "the the quick brown fox  fox jumps over the lazy dog dog.";

            // Find matches.
            MatchCollection matches = rx.Matches(text);

            // Report the number of matches found.
            Console.WriteLine("{0} matches found in:  {1}",
                              matches.Count,
                              text);

            // Report on each match.
            foreach (Match match in matches)
            {
                GroupCollection groups = match.Groups;
                Console.WriteLine("'{0}' repeated at positions {1} and {2}",
                                  groups["word"].Value,
                                  groups[0].Index,
                                  groups[1].Index);
            }
            Console.ReadKey();
        }
    }
}

Output:

3 matches found in:   the the quick brown fox  fox jumps over the lazy dog dog.
'the' repeated at positions 0 and 4
'fox' repeated at positions 20 and 25
'dog' repeated at positions 49 and 53

regex-example-c-sharp-min.png

Validate email address using Regex in C#

We can also validate email address using regex pattern in C#

using System;
using System.Text.RegularExpressions;

namespace RegexExamples
{
    class Program
    {
        static void Main(string[] args)
        {
            var EmailId1 = "test@gmail.com";
            var EmailId2 = "test1212@com";

            string pattern = @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z";

            ValidateEmail(EmailId1,pattern);
            ValidateEmail(EmailId2, pattern);

            Console.ReadKey();
        }

        public static void ValidateEmail(string Email, string pattern)
        {
            //check second string
            if (Regex.IsMatch(Email, pattern))
            {
                //print if email id is valid
                Console.WriteLine(Email + " is Valid Email address ");
            }
            else
            {
                //print if email id is not valid
                Console.WriteLine(Email + " is Not a Valid Email address");
            }
        }
    }
}

Output:

test@gmail.com is Valid Email address
test1212@com is Not a Valid Email address

Replace Special Characters from String using Regex in C#

Considering the code in the answer for a question, remove special characters from string using Regex in C#

using System;
using System.Text.RegularExpressions;

namespace RegexExamples
{
    class Program
    {
        static void Main(string[] args)
        {
            var StringToBeRepalced = "test@ Suite #218 ";

            var StringWithoutSpclCharac = Regex.Replace(StringToBeRepalced, @"[^0-9a-zA-Z\._]", " ");

            Console.WriteLine(StringWithoutSpclCharac);

            Console.ReadKey();
        }

       
    }
}

Output:

test  Suite  218

You may also like to read:

C# Collections

C# List

Convert List to Dictionary in C# (Various ways)

Remove All WhiteSpace from string in C# (Multiple Methods)

Convert String to List in C# and string array to list

Convert List to string in C# (Comma-separated, New-Line)