Email address check function

Andrew M. Kuchling akuchlin at mems-exchange.org
Thu Dec 2 13:47:29 EST 1999


Gerrit Holl <gerrit.holl at pobox.com> writes:
> I'm writing some CGI scripts and I want the user to fill in their real email
> address. Checking this is more difficult than just look if it contains an '@'.

Definitely.  In _Mastering Regular Expressions_, Jeffrey Frield
derives a regular expression for matching valid RFC-822 email
addresses.  Perl versions can be downloaded from 
http://public.yahoo.com/~jfriedl/regex/code.html .  The version in the
PCRE test suite begins:

/[\040\t]*                    # Nab whitespace.
(?:
\(                              #  (
[^\\\x80-\xff\n\015()] *                             #     normal*
(?:                                 #       (
(?:  \\ [^\x80-\xff]  |
\(                            #  (
[^\\\x80-\xff\n\015()] *                            #     normal*
(?:  \\ [^\x80-\xff]   [^\\\x80-\xff\n\015()] * )*        #     (special normal*)*
\)                           #                       )
)    #         special
[^\\\x80-\xff\n\015()] *                         #         normal*
)*                                  #            )*
\)                             #                )
[\040\t]* )*    # If comment found, allow more spaces.

... and so on, lasting for 593 lines in all.  But of course you can't
verify an address like foo at bar.com without actually sending e-mail to
it and seeing if it bounces.

-- 
A.M. Kuchling			http://starship.python.net/crew/amk/
    "Aha! I knew I'd find you here!"
    "Huh? Well, good for you. Now it's your turn to hide."
    -- Weevil Dendrite and Ambrose Bierce in STANLEY AND HIS MONSTER #2





More information about the Python-list mailing list