4

Duplicate: Using a regular expression to validate an email address


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) dialect of regular expressions.

Community
  • 1
  • 1
AndyM
  • 3,574
  • 6
  • 39
  • 45
  • Please see that one [question](http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses). – Keltia Jan 16 '09 at 15:21

7 Answers7

6

This question has been asked and answered several times:

Using a regular expression to validate an email address

Why are people using regexp for email and other complex validation?

Regexp recognition of email address hard?

Specifically related to .NET:

Validating e-mail with regular expression VB.Net

Community
  • 1
  • 1
Aron Rotteveel
  • 81,193
  • 17
  • 104
  • 128
1

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
  • 11,015
  • 1
  • 30
  • 44
  • 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 Jan 16 '09 at 15:26
1

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
  • 39,306
  • 13
  • 73
  • 89
1

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
  • 9,457
  • 5
  • 39
  • 35
0

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"

Community
  • 1
  • 1
Harald Schilly
  • 1,098
  • 1
  • 14
  • 15
  • why would that required the tld's inside it? what about .ca and .uk for instance? – jdkoftinoff Jan 16 '09 at 15:24
  • 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 Jan 16 '09 at 15:29
  • 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. Jan 16 '09 at 15:32
0
[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.
  • 3,888
  • 3
  • 24
  • 31
0

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.
  • 113,939
  • 20
  • 158
  • 187