开发者

Is this email regex complete [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment开发者_运维技巧 in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

I'm following these specifications from wikipedia.

[^\.]([a-zA-Z\d\!\#\$%&\'\*\+\-\/\=\?\^_`\{|\}\~]|[^\.][\.]{1})+[^\.]@[a-zA-Z\d\-\_]+(\.[a-z]{2,5}){1,2}

How can it be improved/shortened?

PS:

I know there are multitudes of email validators already made, this is strictly for my own learning in regards to regex. Thanks.


Domain names cannot contain underscores, so you should remove this from the part after the @.

What about sub-domains? I don't think the given regex will match someone@subdomain.example.com

Personally, I've always used /^[a-zA-Z0-9._%+-]+@([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,6}$/ which does not match the entire RFC-2822 specification, but does the job for >99.9% of real-world email addresses.


If you want to learn about validating email addresses with regular expressions and some of the trade-offs, read this article http://www.regular-expressions.info/email.html

Another good source are widely used open source libraries or applications that contain functions for validating email.

Your regex indeed doesn't match emails with subdomains, which you can achieve by adding the dot to the 1st character class after the @ sign

[^\.]([a-zA-Z\d\!\#\$%&\'\*\+\-\/\=\?\^_`\{|\}\~]|[^\.][\.]{1})+[^\.]@[a-zA-Z\d\-\_\.]+(\.[a-z]{2,5}){1,2}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜