2

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.

neo
  • 1,260
  • 11
  • 22

5 Answers5

5

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!

Community
  • 1
  • 1
Scott
  • 9,458
  • 7
  • 54
  • 81
3
"foo bar"@example.com

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

Joey
  • 344,408
  • 85
  • 689
  • 683
3

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
  • 25,229
  • 9
  • 75
  • 117
1
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
  • 328,213
  • 58
  • 503
  • 561
0

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

Rob
  • 6,731
  • 12
  • 52
  • 90
  • 3
    I doubt the problem was trying out the RE but rather finding examples where it doesn't match ... – Joey May 18 '10 at 13:06