VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/what-is-regular-expression-in-c-sharp/

⇱ What is Regular Expression in C#? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

What is Regular Expression in C#?

Last Updated : 12 Jul, 2025

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. 

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.

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:

Output:

Match Value: aaaab


Example 2:

Output:

Match Value: aaab


Example 3:

Output:

Match Value: ab


Special Characters

Sub-expressionMatches
^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).
\dIt is use to match the digit character.
\DIt is use to match the non-digit character.
\wIt is use to match any alphanumeric and underscore character.
\WIt is use to match the any non-word character.
\sIt is use to match the white-space characters.
\SIt is use to match the non white-space characters.
\nIt is use to match a newline character.


Example 1:

Output:

Match Value: Shyam


Example 2:

Output:

Match Value: Parth


Example 3:

Output:

Match Value: seat


Example 4:

Output: 

Match Value: 1


Character Classes

Sub-expressionMatches
[]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:

Output:

Match Value: a


Example 2:

Output:

Match Value: x


Example 3:

Output:

Match Value: m


Grouping and Alternatives

Sub-expressionMatches
()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:

Output:

Match Value: cdcd


Example 2:

Output:

Match Value: e


 

Comment
Article Tags:
Article Tags:

Explore