How to Validate Email Addresses With Regular Expressions

by Ryan Flynn

Don't bother.

Valid email addresses can have so many forms that it's impractical to use a regex complex enough to handle RFC 822 Email Addresses.

What you really care about is

  1. communication with whoever's on the other end
  2. protecting your email system from attack

Instead of trying an exact positive match, do the opposite and eliminate obvious negatives:

  1. Absence of "\r" and "\n" (these are used to separate email headers)
  2. Presence of one or more "@"
  3. Send the address a special link in an email and have them click it.

Given potential email address e

if e.count("\r") == 0 and e.count("\n") == 0 and "@" in e:
  send_validation_email(e)