正则表达式:仅允许在域和顶级域中间使用连字符的电子邮件验证 [英] Regex: Email validation that allows only hyphens in the middle of the domain and top-level domain
问题描述
我知道这之前被问过很多次,但我没有找到任何真正满足电子邮件地址必须满足的所有条件才能被视为有效的内容.
I know this is been asked tons of times before , but I haven't found anything that really meets all the conditions that an email address must meet to be considered valid.
考虑以下作为电子邮件地址的结构:
Considering the following as the structure of an email address :
part1@part2.part3.part4
part1=用户名
part1=username
part2=域
part3 和 part4 = 顶级域
part3 and part4 =top-level domain
这些是必须满足的所有条件:
These are all the conditions that must be met:
- 电子邮件地址不得接受空格
- 电子邮件地址不得以点或字母或数字以外的字符结尾
- 只允许一个@符号
- at 符号前后不能有特殊字符
- 域点(电子邮件地址的 part2 之后的点)前后不能有特殊字符
- 您不能在用户名中连续输入两个或多个点
- 在域中,@和点之间,@和点旁边的字符必须是字母或数字,中间唯一允许的特殊字符是连字符.
- 第 7 步中的顶级域(第 3 部分和第 4 部分或电子邮件)也是如此
这是我目前使用的正则表达式:
This is the regular expression I currently using :
^([w.-]+)@([w-]+)((.(w){2,9})+)$
但不满足条件:4、5、6、7、8
But it does not meet conditions :4,5,6,7 and 8
我只是想弄清楚如何补充我的正则表达式并在这个过程中学习.
I'm just trying to figure out how to complement my regular expression and learn in the process.
这是无效电子邮件列表
mkyong – 必须包含@"符号
mkyong – must contains "@" symbol
mkyong123@.com – 域名不能以点."开头
mkyong123@.com – domain can not start with dot "."
mkyong()*@gmail.com – 电子邮件只允许使用字符、数字、下划线和破折号
mkyong()*@gmail.com – email’s is only allow character, digit, underscore and dash
mkyong@%*.com – 电子邮件的 tld 只允许字符和数字
mkyong@%*.com – email’s tld is only allow character and digit
mkyong..2002@gmail.com – 双点."不允许
mkyong..2002@gmail.com – double dots "." are not allow
mkyong.@gmail.com – 电子邮件的最后一个字符不能以点."结尾
mkyong.@gmail.com – email’s last character can not end with dot "."
mkyong@mkyong@gmail.com – 不允许使用双@"
mkyong@mkyong@gmail.com – double "@" is not allow
mkyong@gmail.com.1a -email 的 tld 有两个字符不能包含数字
mkyong@gmail.com.1a -email’s tld which has two characters can not contains digit
有效:
mkyong@yahoo.com
mkyong@yahoo.com
mkyong-100@yahoo.com
mkyong-100@yahoo.com
mkyong.100@yahoo.com
mkyong.100@yahoo.com
mkyong111@mkyong.com
mkyong111@mkyong.com
mkyong-100@mkyong.net
mkyong-100@mkyong.net
mkyong.100@mkyong.com.au
mkyong.100@mkyong.com.au
mkyong@1.com
mkyong@1.com
mkyong@gmail.com.com
mkyong@gmail.com.com
mkyong-100@yahoo-test.com
mkyong-100@yahoo-test.com
推荐答案
根据您的有效和无效电子邮件地址列表,这是我所能做到的最好的:
This is the best I have been able to do as per your list of valid and invalid email addresses:
^([w-]|(?<!.).)+[a-zA-Z0-9]@[a-zA-Z0-9]([w-]+)((.([a-zA-Z]){2,9})+)$
更新:
^([w-]|(?<!.).)+[a-zA-Z0-9]@[a-zA-Z0-9]([a-zA-Z0-9-]+)((.([a-zA-Z]){2,9}){0,2})$
这篇关于正则表达式:仅允许在域和顶级域中间使用连字符的电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!