Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Valid Email Format RegEx (See related posts)

/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/

Comments on this post

johanges posts on Feb 04, 2009 at 13:31
Don't take offense, but I suggest you don't use this. It is not complete, has various bugs (i.e., might let through some invalid email addresses) and is incomplete (i.e., it will not recognize otherwise valid addresses).

Validating email addresses is notoriously difficult and should probably never even be attempted with a regular expression. Instead, best practices is to use a library provided by the environment you are using. Perl, Python, Java, Ruby, etc., all have such libraries and they are both faster and more reliable than using a reg-ex. As an added bonus, these have been tested and used in real-world applications and are thus less likely to bite you when you don't expect it.

Now, don't just take my word for it, but check out the little nightmare that is RFC2822 (the rule that govern email formats on the internet) at http://www.faqs.org/rfcs/rfc2822.html, or read advice from sources such as ybiC's "Don't Use Regular Expressions To Parse IP Addresses!" at http://www.perlmonks.org/?node_id=221512

For an interesting example of hard this can be, check out "Mastering Regular Expressions/3e", by Jeffrey Friedl. He has a short example in Chapter 5 on how to match an IP address that ends up a couple of lines long (and I'm still not convinced he gets IPv6 quite right). So that doesn't even cover domain names or what's on the left side of the @.

All in all a very hard problem.

You need to create an account or log in to post comments to this site.