Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for email, why doesn't it work? [duplicate]

Tags:

regex

email

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?

like image 671
hampusohlsson Avatar asked Sep 02 '26 13:09

hampusohlsson


1 Answers

^[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.

like image 119
connec Avatar answered Sep 05 '26 04:09

connec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!