Regular expressions in C#

First of all you need to specify the using keyword on the Text.RegularExpressions namespace so that you will not be required to qualify declarations in those namespaces later in your code. The using statement must be used prior to any other declarations:

using System.Text.RegularExpressions;
Define a new regular expression that will use a pattern match to validate an e-mail address. The following regular expression is structured to accomplish three things:

Capture the substring before the @ symbol and put that into the “user” group.
Capture the substring after the @ symbol and put that into the “host” group.
Make sure that the first half of the string does not have an @ symbol.

Regex emailregex = new Regex(“(?<user>[^@]+)@(?<host>.+)”);

Define a new string containing a valid e-mail address. This provides a default value if the method’s command-line argument is empty:

 

String s = “johndoe@tempuri.org“;
 

Check to see if there are any command-line parameters; if there are, retrieve the first parameter and assign it to the variable “s”.

 

if ( args.Length > 0 ) {
 s = args[0];
}
 

Use the Match method to pass in the e-mail address variable and return a new Match object. The Match object will return regardless of whether any matches were found in the source string.

 

Match m = emailregex.Match(s);
 

By examining the Success property, we can decide whether to continue processing the Match object or to print an error message. If successful, display the “user” and “host” named groups within the Groups collection of the Match object.

 

if ( m.Success ) {
 Console.WriteLine(“User: ” + m.Groups[“user”].Value);
 Console.WriteLine(“Host: ” + m.Groups[“host”].Value);
 } else {
 Console.WriteLine(s + ” is not a valid email address”);
}
Console.WriteLine();

Advertisements

About Saad Khan

Saad is an ASP.NET developer with 4 years of experience, and has also made a number of contributions to the Sitefinity marketplace and community. He loves to explore new code, and create innovative tools and procedures while exploring new approaches in his work with Sitefinity and MVC. As an experienced .NET developer, he frequents both Telerik and Microsoft technologies. In his spare time, Saad is an avid gamer and movie fanatic, and also experiments with new and interesting tools on the Microsoft platform.​​

3 comments

  1. Can anyone plz give me the exact regular expression for email validation?

    • Try this
      Expression = @”^(([^()[\]\\.,;:\s@\””]+”
      + @”(\.[^()[\]\\.,;:\s@\””]+)*)|(\””.+\””))@”
      + @”((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}”
      + @”\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+”
      + @”[a-zA-Z]{2,}))$”;

  2. nice information but i must say at client side use Javascript to validate …

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: