5

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.

1
  • Please see that one question. Commented Jan 16, 2009 at 15:21

7 Answers 7

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

Sign up to request clarification or add additional context in comments.

1 Comment

2

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])? 

1 Comment

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.
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.

Comments

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.

Comments

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"

3 Comments

why would that required the tld's inside it? what about .ca and .uk for instance?
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.
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.
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.

1 Comment

or just use it in case-insensitive mode
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}$

1 Comment

Why [\w-\.]+?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.