Regex Validating CSV Email Addresses
Introduction
One of the most strengths added to Microsoft .NET framework is Regular expressions language, Actually I have passion to regular expressions, I used it intensively to build a meta search engine, and you can do more with less when you are using regular expressions (a.k.a.: Regex), most of times I find myself going to this solution even in non-technical situations; it helps so much to get away from string parsing.
Regex Tools and Resources
One of the earliest sites, that has been launched since .NET 1.0 is RegexLib , it's so helpful and it contains a repository of Regular expressions for any pattern you will ever need, and it has a test module to test your Regex against input text you supply.
Learning Regex is somehow painful, but once you get acquainted it will be a real fun!. so you will find so many tools helping you to build and test your own Regular expressions.
One of the earliest tools is Expresso, which started as a code project article then it has been so popular and usable by most of .NET Geeks.
Regulator, is one of the most cool tools, it has a builder like Expresso and also it has a great intellisense feature that allows you to remember Regex syntax easily, and imagine what it has a search engine that lets you search in RegexLib repository (actually it's a webservice call sent to RegexLib.com) so you are not asked to start building your Regex from scratch!.
Common feature between Expresso & Regulator both of them provides reverse text feature it means if you supply one Regex as : \d+ , it generates a description like : " matches any non-zero length digit " , ain't it cool !.
You will find so many articles about how to get your hands on Regular Expression Language, check these links.
Case Study
Problem
You have a comma separated group of email addresses and you want to make sure that each email address is valid one.
Design
Regular expression is the most adequate solution for such a case, so I will use regular expression to make sure each address is a valid one, so the first step is how to build this regular expression, a prerequisite is that you are familiar with regular expression as a language, otherwise I suggest that you search RegexLib.com.
Solution
Building Regular Expression
After some iterations with Expresso, I deducted the following:
Input Sample : kareem@yahoo.com,Mike@gmail.com,Michael@hotmail.com
Objective : making sure that each of these addresses is a valid one.
Regex : [a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,5}
Let's have a look at this regular expression, I will follow divide and conquer to explain each piece of this regex
[a-zA-Z0-9_\-\.]+ : means any character lower or upper case, or any digit from 0 to 9 and also "_" is allowed, also "-" or ".", the + means this cannot be optional you should supply non-zero length string before the @ sign.
@ : each and every email address should have the @ sign.
[a-zA-Z0-9_\-\.]+ : explained above.
\. : It's the . that's followed by com, org, net, etc..
[a-zA-Z]{2,5} : this is the com, net, biz, etc.. , it's any character upper or lower case and the length should be between 2 and 5.
C# Code To Access Matched Addresses
You can access the matched email address using the following snippet
Regex matchRegex = new Regex(@"[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,5}");
MatchCollection matches =
matchRegex.Matches("zako@hotmail.com, weko2@yhaoo.com,kareem@gmail.com");
if ( matches.Count > 0)
{
for ( int i = 0; i <>{// You can process the email matched
// for example you can check if it's a real email address or not.
// by pinging the mail provider.
ProcessEmailAddress(matches[i].Value);}}
Note: don't forget to add the inclusive directive using System.Text.RegularExpressions;