3

Possible Duplicate:
What is the best regular expression for validating email addresses?

I've been trying to do some regex for email filtering.

Allowed

[email protected]
[email protected]
[email protected]

Not allowed

[email protected]
[email protected]
[email protected]
@example.com

This one accepts all allowed... but also [email protected]

^([a-zA-Z0-9]+[\.]?){1,2}@([a-zA-Z0-9]+\.)+(com)$

This one accepts all allowed... except [email protected]!

Added [^\.] before @

^([a-zA-Z0-9]+[\.]?){1,2}[^\.]@([a-zA-Z0-9]+\.)+(com)$

What am I missing here?

Community
  • 1
  • 1
hampusohlsson
  • 10,109
  • 5
  • 33
  • 50

1 Answers1

2

^[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)?@([a-zA-Z0-9]+\.)+com$ will accept your 'allowed' emails and reject the others.

Naturally, that will not match all generally valid emails. See http://www.regular-expressions.info/email.html for a discussion on email regular expressions.

connec
  • 7,231
  • 3
  • 23
  • 26