C# Regular Expression with Examples

In C #, a regular expression is a pattern used to parse the specified input pattern and check if the specified input text matches the specified pattern. In C #, regular expressions are usually called C # Regex.

We have the regular expression engine in the .NET framework which provides pattern matching Patterns that can consist of any literary character, operator, or builder. C # provides a class named Regex which is found in the space name System.Text.RegularExpression.

This class will do two things: Parse the input text for regular expression patterns. It recognizes regular expression patterns in the specified text.

Example:

static void Main(string[] args)
{
	string[] emailaddress = {"[email protected]",
			  "shahzadualberta.net"};

	foreach (string emails in emailaddress)
	{
		Console.WriteLine("{0} {1} a valid E-mail address.", emails,
							ValidateEmail(emails) ? "is" : "is not");
	}

	}
	public static bool ValidateEmail(string EmailtoValidate)
	{
		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(EmailtoValidate))
			return (true);
		else
			return (false);
	}
}

Output:

[email protected] is a valid E-mail address.
shahzadualberta.net is not a valid E-mail address.

Syntax of Regex:

A number regex basic syntax in C#

  • Quantifiers
  • Special Characters
  • Character Classes
  • Grouping and alternatives

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.

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(newline).
\d It is used to match the digit character.
\D It is used to match the non-digit character.
\w It is used to match any alphanumeric and underscore character.
\W It is used to match any non-word character.
\s It is used to match the white-space characters.
\S It is used to match the non-white-space characters.
\n It is used to match a newline character.

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 the Escaped special character.

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 the expression is matched it gives yes otherwise it gives no