Don't use regular expressions to "validate" email addresses (was: I need some help with a regexp please)

Ben Finney bignose+hates-spam at
Fri Sep 22 03:06:31 CEST 2006

"John Machin" <sjmachin at> writes:

> A little more is unfortunately not enough. The best advice you got was
> to use an existing e-mail address validator. The definition of a valid
> e-mail address is complicated. You may care to check out "Mastering
> Regular Expressions" by Jeffery Friedl. In the first edition, at least
> (I haven't looked at the 2nd), he works through assembling a 4700+ byte
> regex for validating e-mail addresses. Yes, that's 4KB.  It's the best
> advertisement for *not* using regexes for a task like that that I've
> ever seen.

The best advice I've seen when people ask "How do I validate whether
an email address is valid?" was "Try sending mail to it".

It's both Pythonic, and truly the best way. If you actually want to
confirm, don't try to validate it statically; *use* the email address,
and check the result.  Send an email to that address, and don't use it
any further unless you get a reply saying "yes, this is the right
address to use" from the recipient.

The sending system's mail transport agent, not regular expressions,
determines which part is the domain to send the mail to.

The domain name system, not regular expressions, determines what
domains are valid, and what host should receive mail for that domain.

Most especially, the receiving mail system, not regular expressions,
determines what local-parts are valid.

 \       "I believe in making the world safe for our children, but not |
  `\    our children's children, because I don't think children should |
_o__)                                  be having sex."  -- Jack Handey |
Ben Finney

More information about the Python-list mailing list