What is Regular Expression in C#?
Last Updated :
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 Expressions
using 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 Expressions
using System;
using System.Text.RegularExpressions;
class GFG {
// Main Method
static void Main(string[] args)
{
// Input strings for Match
// valid E-mail address.
string[] str = {"[email protected]",
"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:
[email protected] is a valid E-mail address.
parthmaniyargmail.com is not a valid E-mail address.
@gmail.com is not a valid E-mail address.
Regex Syntax
There are many basic syntaxes like Quantifiers, Special Characters, Character Classes, Grouping & Alternatives are used for regular expressions.
Quantifiers:
Sub-expression(Greedy) | Sub-expression(Lazy) | Matches |
---|
* | *? | Used to match the preceding character zero or more times. |
+ | +? | Used to match the preceding character one or more times. |
? | ?? | Used to match the preceding character zero or one time. |
{n} | {n}? | Used to match the preceding character exactly n times. |
{n, } | {n, }? | Used to match the preceding character at least n times. |
{n, m} | {n, m}? | Used to match the preceding character from n to m times. |
Example 1:
C#
// C# program to demonstrate
// the * Quantifier
using 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 + Quantifier
using 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 ? Quantifier
using 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
Sub-expression | Matches |
---|
^ | Word after this element matches at the beginning of the string or line. |
$ | Word before this element matches at the end of the line or string. |
.(Dot) | Matches any character only once expect \n(new line). |
\d | It is use to match the digit character. |
\D | It is use to match the non-digit character. |
\w | It is use to match any alphanumeric and underscore character. |
\W | It is use to match the any non-word character. |
\s | It is use to match the white-space characters. |
\S | It is use to match the non white-space characters. |
\n | It is use to match a newline character. |
Example 1:
C#
// C# program to demonstrate
// the ^ Special Character
using 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 Character
using 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 Character
using 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 Character
using 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
Sub-expression | Matches |
---|
[] | It is used to match the range of character |
[a-z] | It is used to match any character in the range of a-z. |
[^a-z] | It is used to match any character not in the range of a-z. |
\ | It is used to match Escaped special character. |
Example 1:
C#
// C# program to demonstrate
// the [] character class
using 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 class
using 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 class
using 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
Sub-expression | Matches |
---|
() | It is used for group expression |
(a|b) | | Operator is used for alternative either a or b. |
(?(exp) yes|no) | If expression is matched it gives yes otherwise it gives no. |
Example 1:
C#
// C# program to demonstrate
// the grouping in regex
using 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 regex
using 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
Similar Reads
Expression-Bodied Members in C#
Expression-bodied members provide a minimal and concise syntax to define properties and methods. It helps to eliminate boilerplate code and helps writing code that is more readable. The expression-bodied syntax can be used when a member's body consists only of one expression. It uses the =>(fat a
7 min read
List.FindIndex() Method in C# with Examples
List<T>.FindIndex Method is used to search for an element that matches the conditions defined by a specified predicate and returns the index of the first occurrence within the List<T>. If an item that matches the conditions is not found then this method will return -1. There are 3 method
6 min read
Uri.HexUnescape() Method in C# with Examples
Uri.HexUnescape() method is used to convert a specified hexadecimal representation of a character to the character. Syntax: public static char HexUnescape (string pattern, ref int index); Parameters: string str â This represents the hexadecimal string.ref int index â This represents location in patt
2 min read
C# | Char.IsSurrogatePair(String, Int32) Method
This method is used to indicates whether two adjacent Char objects at a specified position in a string form a surrogate pair or not. Syntax: public static bool IsSurrogatePair (string s, int index); Parameters: s: It is a String. index: It is the starting position of the pair of characters to evalua
4 min read
LINQ | Quantifier Operator | Contains
In LINQ, quantifier operators are used to returning a boolean value which shows that whether some or all elements satisfies the given condition. The Standard Query Operator supports 3 different types of quantifier operators: All Any Contains Contains Operator The Contains operator is used to check w
4 min read
C# | Char.IsSurrogate(String, Int32) Method
This method is used to indicates whether the character at the specified position in a specified string has a surrogate code unit or not. Syntax: public static bool IsSurrogate (string s, int index); Parameters: s: It is a String. index: It is the character position to evaluate in s. Return Value: Th
4 min read
StringBuilder.ToString Method in C#
This method is used to converts the value of this instance to a String. A new String object is created and initialized to get the character sequence from this StringBuilder object and then String is returned by ToString(). Subsequent changes to this sequence contained by Object do not affect the con
2 min read
C# | TrimStart() and TrimEnd() Method
Prerequisite: Trim() Method in C# In C#, TrimStart() & TrimEnd() are the string methods. TrimStart() method is used to remove the occurrences of a set of characters specified in an array from the starting of the current String object. TrimEnd() method is used to remove the occurrences of a set o
3 min read
Pattern Matching in C#
Pattern matching is a feature that allows testing an expression for the occurrence of a given pattern. It is a feature more prevalent in functional languages. Pattern matching is Boolean in nature, which implies there are two possible outcomes: either the expression matches the pattern or it does no
15+ min read
C# | Uri.IsHexEncoding() Method
Uri.IsHexEncoding(String, Int32) Method is used to check whether a character in a string is hexadecimal encoded or not. This checks for hexadecimal encoding which follows the pattern "%hexhex" in a string, where "hex" is a digit from 0 to 9 or a letter from A-F (case-insensitive). Syntax: public sta
2 min read