views:

106

answers:

5

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.

+3  A: 
"foo bar"@example.com

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

Joey
A: 

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

Rob
I doubt the problem was trying out the RE but rather finding examples where it doesn't match ...
Joey
+4  A: 

Check out this post:

http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses

There is also this one:

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

Nothing like a 6000+ character regex!

Scott
He does not ask for a regex to match all and only all valid e-mail addresses.
Jens
+1  A: 
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.

Tim Pietzcker
+3  A: 

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

/.+@.+/
Jens