Is regular expression recognition of an email address hard?

email, regex, textmatching, validation

Solution

For the formal e-mail spec, yes, it is technically impossible via Regex due to the recursion of things like comments (especially if you don't remove comments to whitespace first), and the various different formats (an e-mail address isn't always someone@somewhere.tld). You can get close (with some massive and incomprehensible Regex patterns), but a far better way of checking an e-mail is to do the very familiar handshake:

- they tell you their e-mail

- you e-mail them a confimation link with a Guid

when they click on the link you know that:

- the e-mail is correct

- it exists

- they own it

Far better than blindly accepting an e-mail address.

Problem

I recently read somewhere that writing a regexp to match an email address, taking into account all the variations and possibilities of the standard is extremely hard and is significantly more complicated than what one would initially assume. Why is that? Are there any known and proven regexps that actually do this fully? What are some good alternatives to using regexps for matching email addresses?

Original source

Related problems