Put simply: “YOU DON’T” so quit trying.

Just today:
http://stackoverflow.com/questions/5806520/parse-email-header-with-regex-in-c
http://thedailywtf.com/Articles/Email-Validation-Validity.aspx

Why people insist on attempting this is beyond me. I’ve grown to really like Regex, I’ve even built a light-weight xml parser on regular expressions but you can not parse email addresses with a regex. Just look at some of these crazy attempts in the daily wtf comments.

If you want to validate an email use:

 try
{ new System.Net.Mail.MailAddress(email); return true; }
catch(FormatException)
{ return false; }

If you MUST use a regular expression on the client for email validation, try this one:

.+@.+

More Info:
http://tools.ietf.org/html/rfc2822#section-3.4.1

3.4.1. Addr-spec specification

An addr-spec is a specific Internet identifier that contains a locally interpreted string followed by the at-sign character (“@”, ASCII value 64) followed by an Internet domain.

Yes, locally interpreted, meaning you can’t. Yes the RFC does state some constraints, but they are too complex to express in a regex. Get a library or don’t validate it.

Comments