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.
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.
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])?
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"
[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.
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.
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.
This question has been asked and answered several times:
http://stackoverflow.com/questions/156430/regexp-recognition-of-email-address-hard
Specifically related to .NET:
http://stackoverflow.com/questions/369543/validating-e-mail-with-regular-expression-vb-net
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.
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}$