tags:

views:

825

answers:

9

Duplicate: http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses


There seem to be an awful lot of different variants on this on the web and was wondering if there is a definitive answer?

Preferably using the .net (Regex) dialog of regular expressions.

+1  A: 

regular-expressions says that this one matches about 99%

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
Aif
I hate that linked page, and the authors idea of what acceptable trade-offs are. That is one of the better versions of the regex he lists though.
SpoonMeiser
+2  A: 

Please see that one question.

Keltia
A: 

I've had the same problem some time ago. RFC 2822 defines it and according to this page this one is useful and is the one i picked: "[a-z0-9!#$%&'+/=?^{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*@(?:a-z0-9?.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)\b"

Harald Schilly
why would that required the tld's inside it? what about .ca and .uk for instance?
jdkoftinoff
Also, .asia. Trying to ensure the domain is valid inside of a regex is an exercise in futility and not what a regex is meant to do. A regex should only ensure it is in a valid format.
SpoonMeiser
Those are only good for matching *US* addresses. While you're at it, you should just ping the domain and check it's valid. Then you don't have to make a regex that contains every suffix imaginable.
lc
A: 
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

Probably want to add A-Z next to all the lower case versions in order to allow uppercase letters as well.

Ty
or just use it in case-insensitive mode
dancavallaro
+1  A: 

The definitive answer? Or the normal answer?

I ask because the formal email address specification allows all sorts of weird things (parenthesis, quoted phrases, etc) that most people don't bother to account for.

See this page for a list of both comprehensive and normal regex'es.

BradC
+1  A: 

I don´t think there´s a silver bullet for email regex verification.

what people are commonly doing is to verify only for mistakes, like the absence of @ and one dot. And then send a email verification to that address. It´s the only way to be sure that they email is actually valid.

Tiago
A: 

Have a look here for a short answer. However this is something that is difficult to do 100% right with regular expressions. See here for details.

kgiannakakis
A: 

I don't know if there's one definitive answer for this one, but if you put aside actually checking if the domain exists, email addresses boil down to <username>@<domain>, where <domain> contains at least one dot and two to four characters in the suffix. You can do all kinds of things to check for illegal/special characters, but the simplest one would be:

^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$
lc