Regular expressions and validate email address in C#

chris (2005-02-16 17:38:36)
35595 views
6 replies

Regular expressions in C#/.NET are provided by the System.Text.RegularExpressions namespace. This makes form validation and pattern matching an easy job for the C# developer. In this example I just want to check to see if a string is a valid email address.

At the start of the program you will have to call the required namespace like so:
using System.Text.RegularExpressions;

Then in your code the first thing to do is to define the string which you think might be an email address:
string _address = "channels@spiration.co.uk";
Regex emailregex = new Regex("(?<user>[^@]+)@(?<host>.+)");

The following line creates a Regular Expression object called emailregex. This class takes an expression in it's constructor as shown above. Note that it is also possible to do sub-string capturing using the <string> syntax. These substrings are then available in the m.Groups array (for example m.Groups["user"] and m.Groups["host"].

So moving on, the next stage is to create a Match object, which we can use to tell whether or not the regular expression was matcyhed. The match object is set by calling the Match method on the expression and passing in the address.
Match m = emailregex.Match(_address);

from that point, you can then test the result with a simple conditional statement:
if ( m.Success ) {
   // success,, so do something;
}else{
   // didn't manage to find the regular expression
}

That's really all there is to it.

christo
Digg it! Submit to Slashdot Add to Blinklist Del.icio.us Add to Newsvine Add to Technorati Add it to Google Bookmarks
comment
anonymous
2009-01-06 06:06:11

Thanks

thanks. this was great.
reply icon
Dib
2009-02-21 15:36:45

Brilliant!

Hi.

Thanks, just what I needed. Probably saved me hours!

Dib.
reply icon
Vincent Duvernet
2009-06-08 10:43:39

I've found this sample :

http://cairocafe.blogspot.com/2006/05/regex-validating-csv-email-addresses.html

Which is better with bad formatted emails (like t@t.u)
reply icon
Connie
2010-02-26 04:56:16

Thank you!

Regular expressions in C#/.NET are provided by the System.Text.RegularExpressions namespace. This makes form validation and pattern matching an easy job for the C# developer. In this example I just want to check to see if a string is a valid email address.

At the start of the program you will have to call the required namespace like so:
using System.Text.RegularExpressions;

Then in your code the first thing to do is to define the string which you think might be an email address:
string _address = "channels@spiration.co.uk";
Regex emailregex = new Regex("(?<user>[^@]+)@(?<host>.+)");

The following line creates a Regular Expression object called emailregex. This class takes an expression in it's constructor as shown above. Note that it is also possible to do sub-string capturing using the <string> syntax. These substrings are then available in the m.Groups array (for example m.Groups["user"] and m.Groups["host"].

So moving on, the next stage is to create a Match object, which we can use to tell whether or not the regular expression was matcyhed. The match object is set by calling the Match method on the expression and passing in the address.
Match m = emailregex.Match(_address);

from that point, you can then test the result with a simple conditional statement:
if ( m.Success ) {
   // success,, so do something;
}else{
   // didn't manage to find the regular expression
}

That's really all there is to it.

christo


Thank you!! This was the easiest one I found that actually worked.
reply icon
muratos
2010-10-20 17:18:15

Thanks a lot. I use this regexp for email validation now.
reply icon
Jared
2011-08-01 05:58:35

A strict, short, accurate example

Hey, I wrote a strict, short, accurate example of how to verify an email. It is pretty accurate.

Regular Expressions in C# (including a new comprehensive email pattern)

Any feedback would be appreciated.
reply icon