Regular expressions and validate email address in C#

chris (2005-02-16 17:38:36)
11491 views
4 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
comment
anonymous
2009-01-06 06:06:11

Thanks

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

Brilliant!

Hi.

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

Dib.
reply iconedit reply
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 iconedit reply
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 iconedit reply