Tiffany B. Brown

A web log about web development and internet culture with frequent detours into other stuff.
Three-bean, six-spice turkey chili
Links for December 13, 2006

A better RegEx pattern for matching e-mail addresses

UPDATE: Also read Stefan Esser’s post: Holes in most preg_match() filters.

A few weeks ago, I posted a regular expression pattern for matching e-mail addresses.

Below is a more refined version.

^[-+.\w]{1,64}@[-.\w]{1,64}\.[-.\w]{2,6}$

Just as with the previous pattern, this one will match most valid e-mail addresses including:

This pattern takes advantage of the \w character type. It’s a simpler way of waying “a - z (both upper and lower case), 0 - 9 and the underscore character” (though for many languages, \w means any alphanumeric character).

It also checks to see whether a user or domain name contains at least one, but no more than 64 alphanumeric characters. Sixty-four is the maximum character length for user and domain names under SMTP.

This pattern should work with most regular expression engines.

Recommended reading: Mastering Regular Expressions by Jeffrey E. F. Friedl

Share this entry:
  • TwitThis
  • Digg
  • Technorati
  • del.icio.us
  • Ma.gnolia
  • Mixx
  • NewsVine
  • Reddit
  • StumbleUpon
  • TailRank
  • Global Grind
  • YahooMyWeb
  • Google
  • Live
  • LinkedIn
  • MySpace
previous post: Three-bean, six-spice turkey chili
next post: Links for December 13, 2006