What is Regular Expression in C#?

02 Jul, 2021
In C#, Regular Expression is a pattern which is used to parse and check whether the given input text is matching with the given pattern or not. In C#, Regular Expressions are generally termed as C# Regex. The .Net Framework provides a regular expression engine that allows the pattern matching. Patterns may consist of any character literals, operators or constructors. C# provides a class termed as Regex which can be found in System.Text.RegularExpression namespace. This class will perform two things:
Parsing the inputting text for the regular expression pattern.
Identify the regular expression pattern in the given text.
Example 1: Below example demonstrate the use of regex in Mobile Number Verification. Suppose you are making a form where you need to verify the user-entered mobile number then you can use regex. 
C#
// C# program to validate the Mobile// Number using Regular Expressionsusing System;using System.Text.RegularExpressions; class GFG {         // Main Method    static void Main(string[] args)    {        // Input strings to Match        // valid mobile number        string[] str = {"9925612824",          "8238783138", "02812451830"};                 foreach(string s in str)        {            Console.WriteLine("{0} {1} a valid mobile number.", s,                        isValidMobileNumber(s) ? "is" : "is not");        }                 Console.ReadKey();    }         // method containing the regex    public static bool isValidMobileNumber(string inputMobileNumber)    {        string strRegex = @"(^[0-9]{10}$)|(^\+[0-9]{2}\s+[0-9]                {2}[0-9]{8}$)|(^[0-9]{3}-[0-9]{4}-[0-9]{4}$)";                 // Class Regex Represents an        // immutable regular expression.        //   Format                Pattern        // xxxxxxxxxx           ^[0 - 9]{ 10}$        // +xx xx xxxxxxxx     ^\+[0 - 9]{ 2}\s +[0 - 9]{ 2}\s +[0 - 9]{ 8}$        // xxx - xxxx - xxxx   ^[0 - 9]{ 3} -[0 - 9]{ 4}-[0 - 9]{ 4}$        Regex re = new Regex(strRegex);                 // The IsMatch method is used to validate        // a string or to ensure that a string        // conforms to a particular pattern.        if (re.IsMatch(inputMobileNumber))            return (true);        else            return (false);    }}
Output:
9925612824 is a valid mobile number.
8238783138 is a valid mobile number.
02812451830 is not a valid mobile number.
Example 2: Below example demonstrate the use of regex in Email ID Verification. Suppose you are making a form where you need to verify the user-entered email id then you can use regex.
C#
// C# program to validate the Email// ID using Regular Expressionsusing System;using System.Text.RegularExpressions;  class GFG {     // Main Method    static void Main(string[] args)    {          // Input strings for Match        // valid E-mail address.        string[] str = {"parth@gmail.com",                  "parthmaniyargmail.com",                            "@gmail.com"};                 foreach(string s in str)        {            Console.WriteLine("{0} {1} a valid E-mail address.", s,                                isValidEmail(s) ? "is" : "is not");        }             }         // Method to check the Email ID    public static bool isValidEmail(string inputEmail)    {                 // This Pattern is use to verify the email        string strRegex = @"\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";                                   Regex re = new Regex(strRegex, RegexOptions.IgnoreCase);                 if (re.IsMatch(inputEmail))            return (true);        else            return (false);    }}
Output:
parth@gmail.com is a valid E-mail address.
parthmaniyargmail.com is not a valid E-mail address.
@gmail.com is not a valid E-mail address.
 
There are many basic syntaxes like Quantifiers, Special Characters, Character Classes, Grouping & Alternatives are used for regular expressions.
Quantifiers:
Example 1:
C#
// C# program to demonstrate// the * Quantifierusing System;using System.Text.RegularExpressions; class GFG {         // Main Method    public static void Main(string[] args)    {        // This will return any        // pattern b, ab, aab, ...        Regex regex = new Regex(@"a*b");                 Match match = regex.Match("aaaabcd");        if (match.Success)        {            Console.WriteLine("Match Value: " + match.Value);        }    }}
Output:
Match Value: aaaab
Example 2:
C#
// C# program to demonstrate// the + Quantifierusing System;using System.Text.RegularExpressions;  class GFG {         // Main Method    public static void Main()    {                 // this will return any pattern        // like ab, aab, aaab, ....        Regex regex = new Regex(@"a+b");        Match match = regex.Match("aaabcd");        if (match.Success)        {            Console.WriteLine("Match Value: " + match.Value);        }    }}
Output:
Match Value: aaab
Example 3:
C#
// C# program to demonstrate// the ? Quantifierusing System;using System.Text.RegularExpressions;  class GFG {         // Main Method    static void Main()    {                  // This return any pattern like b, ab        Regex regex = new Regex(@"a?b");                 Match match = regex.Match("aaaabcd");                 if (match.Success)        {            Console.WriteLine("Match Value: " + match.Value);        }    }}
Output:
Match Value: ab
Special Characters
Example 1:
C#
// C# program to demonstrate// the ^ Special Characterusing System;using System.Text.RegularExpressions;  class GFG {         // Main Method    static void Main()    {                 // This will return if shyam exist        // at the beginning of the line        Regex regex = new Regex(@"^Shyam");                 Match match = regex.Match("Shyam is my pet name");                 if (match.Success)        {            Console.WriteLine("Match Value: " + match.Value);        }    }}
Output:
Match Value: Shyam
Example 2:
C#
// C# program to demonstrate// the $ Special Characterusing System;using System.Text.RegularExpressions;  class GFG {         // Main Method    public static void Main()    {                 // This return parth if it        // exist at the end of the line        Regex regex = new Regex(@"Parth$");                 Match match = regex.Match("My name is Parth");                 if (match.Success)        {            Console.WriteLine("Match Value: " + match.Value);        }    }}
Output:
Match Value: Parth
Example 3:
C#
// C# program to demonstrate// the .(Dot) Special Characterusing System;using System.Text.RegularExpressions;  class GFG {         // Main Method    static void Main()    {         // This will return any word which         // contains only one letter between         // s and t        Regex regex = new Regex(@"s..t");                 Match match = regex.Match("This is my seat");                 if (match.Success)        {            Console.WriteLine("Match Value: " + match.Value);        }    }}
Output:
Match Value: seat
Example 4:
C#
// C# program to demonstrate// the \d Special Characterusing System;using System.Text.RegularExpressions;  class GFG {         // Main Method    static void Main()    {        // This will the return        // the one digit character        Regex regex = new Regex(@"\d");                 Match match = regex.Match("I am 19 years old");                 if (match.Success)        {            Console.WriteLine("Match Value: " + match.Value);        }             }}
Output: 
Match Value: 1
Character Classes
Example 1:
C#
// C# program to demonstrate// the [] character classusing System;using System.Text.RegularExpressions;  class GFG {         // Main Method    static void Main()    {                 // This will return one character either        // a or b or c which will come first        Regex regex = new Regex(@"[abc]");                 Match match = regex.Match("abcdef");                 if (match.Success)        {            Console.WriteLine("Match Value: " + match.Value);        }    }}
Output:
Match Value: a
Example 2:
C#
// C# program to demonstrate// the [a-z] character classusing System;using System.Text.RegularExpressions;  class GFG {         // Main Method    static void Main()    {                 // This will return any character        // between x and z inclusive        Regex regex = new Regex(@"[x-z]");                 Match match = regex.Match("xmax");                 if (match.Success)        {            Console.WriteLine("Match Value: " + match.Value);        }    }}
Output:
Match Value: x
Example 3:
C#
// C# program to demonstrate// the [^a-z] character classusing System;using System.Text.RegularExpressions;  class GFG {         // Main Method    static void Main()    {                 // This will return other x,        // y and z character        Regex regex = new Regex(@"[^x-z]");                 Match match = regex.Match("xmax");                 if (match.Success)        {            Console.WriteLine("Match Value: " + match.Value);        }    }}
Output:
Match Value: m
Grouping and Alternatives
Example 1:
C#
// C# program to demonstrate// the grouping in regexusing System;using System.Text.RegularExpressions;  class GFG {         // Main Method    static void Main()    {                 // This will return pattern        // will cd, cdcd, cdcdcd, ...        Regex regex = new Regex(@"(cd)+");                 Match match = regex.Match("cdcdde");                 if (match.Success)        {            Console.WriteLine("Match Value: " + match.Value);        }    }}
Output:
Match Value: cdcd
Example 2:
C#
// C# program to demonstrate// the grouping in regexusing System;using System.Text.RegularExpressions;  class GFG {         // Main Method    static void Main()    {                 // This will either d or e        // which ever comes first        Regex regex = new Regex(@"d|e");                 Match match = regex.Match("edge");                 if (match.Success)        {            Console.WriteLine("Match Value: " + match.Value);        }    }}
Output:
Match Value: e
 
ManasChhabra2
sagar0719kumar
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# | Multiple inheritance using interfaces
Differences Between .NET Core and .NET Framework
Extension Method in C#
C# | List Class
C# | .NET Framework (Basic Architecture and Component Stack)
HashSet in C# with Examples
Switch Statement in C#
Lambda Expressions in C#
Partial Classes in C#
Hello World in C#