What is Regular Expression in C#?
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#
using System;
using System.Text.RegularExpressions;
class GFG {
static void Main( string [] args)
{
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();
}
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}$)" ;
Regex re = new Regex(strRegex);
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#
using System;
using System.Text.RegularExpressions;
class GFG {
static void Main( string [] args)
{
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" );
}
}
public static bool isValidEmail( string inputEmail)
{
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.
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#
using System;
using System.Text.RegularExpressions;
class GFG {
public static void Main( string [] args)
{
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#
using System;
using System.Text.RegularExpressions;
class GFG {
public static void Main()
{
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#
using System;
using System.Text.RegularExpressions;
class GFG {
static void Main()
{
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#
using System;
using System.Text.RegularExpressions;
class GFG {
static void Main()
{
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#
using System;
using System.Text.RegularExpressions;
class GFG {
public static void Main()
{
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#
using System;
using System.Text.RegularExpressions;
class GFG {
static void Main()
{
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#
using System;
using System.Text.RegularExpressions;
class GFG {
static void Main()
{
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#
using System;
using System.Text.RegularExpressions;
class GFG {
static void Main()
{
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#
using System;
using System.Text.RegularExpressions;
class GFG {
static void Main()
{
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#
using System;
using System.Text.RegularExpressions;
class GFG {
static void Main()
{
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#
using System;
using System.Text.RegularExpressions;
class GFG {
static void Main()
{
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#
using System;
using System.Text.RegularExpressions;
class GFG {
static void Main()
{
Regex regex = new Regex( @"d|e" );
Match match = regex.Match( "edge" );
if (match.Success)
{
Console.WriteLine( "Match Value: " + match.Value);
}
}
}
|
Output:
Match Value: e
Last Updated :
02 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...