Link to home
Start Free TrialLog in
Avatar of joel011197
joel011197

asked on

How to check an Email address?

I'm not realy confident with expression in Perl to check the validty of string. I would like to check the "validty" (presence of the '@' followed by one or several '.') of
an Email address provided by a user.

What should the expression look like?
if ($email =~ /\@+\.*/) does not match my request because an entry like hello@hello is seen as ok.

Thanks for your help.
Avatar of ozo
ozo
Flag of United States of America image

/\@+\.*/ matches one or more '@'s followd by 0 or more '.'s
so 'hello@hello' is ok, as is 'hello@@...hello'
/\@+.*\./ would fail on 'hello@hello' but match on 'hello@hello.'
But no regular expression can really check the validity of Email addresses, since RFC 822 is so complicated,
and regular expressions can't deal with arbitrarily nested constructs.
(but if you're willing to limit your email addresses to only one level of nesting in comments, then there is a 6598 byte long regular expression that comes close to being able to validate Email address)
ASKER CERTIFIED SOLUTION
Avatar of ptruman
ptruman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> But no regular expression can really check the validity of Email addresses, since RFC 822 is so complicated,

have seen an example for this in Jeffrey Friedl's "Mastering Regular Expressions"

(sorry have this book not handy)
> have seen an example for this in Jeffrey Friedl's "Mastering Regular Expressions"
that example only handles one level of nesting in comments.
(which may be good enough for most purposes)
the example is available at
http://enterprise.ic.gc.ca/~jfriedl/regex/code.html

Thanks for the URL, ozo. I just had the ones from the book, which seem to be outdated.