Question

I tried to create a regular expression which catches all RFC-valid addresses but it's ok if some false-positives come through (though hopefully not so many). This is waht I came up so far:

/^\b\S+@\S+\.[^\s@]{2,}\b$/

Is there any RFC-valid address which doesn't match against this expression or do you have any suggestions to improve it? I don't mind the false positives but I would be glad if you show me a few, too.

Was it helpful?

Solution

E-mail addresses may contain nested comments, which kind of spoils most regex approaches. This is a valid e-mail address:

test(Oh (noes, an @) "sign")@(Here comes the domain)domain.com(TLD is com!!)

Even without comments, the local part may include quoted strings, which may contain whitespace. The best approach I found is: look for an @. Thats mandatory. So I'd use

/.+@.+/

OTHER TIPS

Check out this post:

Using a regular expression to validate an email address

There is also this one:

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

Nothing like a 6000+ character regex!

"foo bar"@example.com

The local part can contain spaces (they have to be quoted, but they are valid).

name@[1.2.3.4]

doesn't match but is valid. A nice list of valid/invalid mail addresses for testing can be found here.

Try regexbuddy to validate reqular expressions. Another handy website i often use is regexplib

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top