NYCPHP Meetup

NYPHP.org

[nycphp-talk] Validating Email Addresses

James B. Wetterau Jr. james at surgam.net
Thu Dec 2 11:19:23 EST 2004


Dan Cech says:
 > Hi All,
 >
 > I've come up against an interesting little problem, and wondered if
 > anyone had any input for me.
...

To validate any potentially valid email address (including those that
use old fashion conventions, multiple subnet parts
(e.g. joe.smith at retail.sales.east.us.example.com), or other stuff that
you don't see frequently requires a pretty daunting regular
expression.  Here's one in Perl regexp language that's supposed to be
RFC compliant: (from the Mail::RFC822::Address module)

http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html

The regexp is:

(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\".\[\] 
\000-\031]+(?:(?:(?:\r\n)?[ \t]
)+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ 
\t]))*"(?:(?:
\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] 
\000-\031]+(?:(?:(
?:\r\n)?[ 
\t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[
\t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] 
\000-\0
31]+(?:(?:(?:\r\n)?[ 
\t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\
](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] 
\000-\031]+
(?:(?:(?:\r\n)?[ 
\t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:
(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ 
\t])+|\Z
|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[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 \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ 
\t])+|\Z
|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ 
\t])*))*\>(?:(
?:\r\n)?[ \t])*))*)?;\s*)





More information about the talk mailing list