正则表达式:电子邮件验证,仅允许域和顶级域中间的连字符 [英] Regex: Email validation that allows only hyphens in the middle of the domain and top-level domain
问题描述
我知道这是以前被问过的次数,但是我没有发现任何真正满足电子邮件地址必须满足的条件被认为是有效的。
考虑以下作为电子邮件地址的结构:
part1@part2.part3.part4
-
part1 =用户名
-
part2 = domain
-
part3和part4 =顶级域名
这些都是必须满足的条件:
- 电子邮件地址不能接受白色空格
- 电子邮件地址不得以字母或数字以外的点或字符结尾
- 只允许一个@符号
- at符号之前或之后不能有特殊字符
- 域点之前或之后不能有特殊字符(点后电子邮件地址的第二部分)
- 您不能输入两个或更多的点在用户名
- 在域中@和点之间,@和点旁边的字符必须是一个字母或数字,在中间唯一允许的特殊字符是连字符。
- 第7步中的顶级域名(第3部分和第4部分或电子邮件)相同。
这是我目前使用的正则表达式:
^ ([\w\.\ - ] +)@([\w\ - ] +)((\。(\w){2,9})+)$
但不符合条件:4,5,6,7和8
我只是想弄清楚如何补充我的正则表达式,并在此过程中学习。
编辑
电子邮件地址中唯一允许的特殊字符是:点,连字符,下划线和at符号
以下是无效电子邮件列表
mkyong - 必须包含@符号
mkyong123 @ .com - 域不能以点。开头。
mkyong()* @ gmail.com - 电子邮件只允许字符,数字,下划线和破折号
mkyong @%*。com - 电子邮件的tld只允许字符和数字
mkyong。 .2002 @ gmail.com - 双点。不允许
mkyong。@ gmail.com - 电子邮件的最后一个字符不能用点结束。 p>
mkyong @ mkyong @ gmail.com - 双@不允许
mkyong@gmail.com.1a -email的tld有两个字符不能包含数字
有效:
mkyong@yahoo.com
mkyong-100@yahoo.com
mkyong.100@yahoo.com
mkyong111@mkyong.com
mkyong-100@mkyong.net
mkyong.100@mkyong.com.au
mkyong@1.com
mkyong@gmail.com.com
mkyong-100@yahoo-test.com
根据您的有效和无效电子邮件地址列表,我已经能够做到这一点:
^([\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})$
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=username
part2=domain
part3 and part4 =top-level domain
These are all the conditions that must be met:
- An email address must not accept white spaces
- An email address must not end in a dot or a character other than a letter or a number
- Only one @ sign is allowed
- There can not be a special character before or after the at sign
- There can not be a special character before or after the domain dot (the dot after part2 of the email address)
- You can not enter two or more dots in a row in the username
- In the domain , between @ and the dot, the characters that are next to the @ and the dot must be a letter or number, in the middle the only special character allowed is the hyphen.
- The same in step 7 goes for the top-level domain(part 3 and part 4 or the email)
This is the regular expression I currently using :
^([\w\.\-]+)@([\w\-]+)((\.(\w){2,9})+)$
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.
EDIT
The only special characters allowed in the email address are : dots, hyphens,underscores and the at sign
Here's a list of invalid emails
mkyong – must contains "@" symbol
mkyong123@.com – domain can not start with dot "."
mkyong()*@gmail.com – email’s is only allow character, digit, underscore and dash
mkyong@%*.com – email’s tld is only allow character and digit
mkyong..2002@gmail.com – double dots "." are not allow
mkyong.@gmail.com – email’s last character can not end with dot "."
mkyong@mkyong@gmail.com – double "@" is not allow
mkyong@gmail.com.1a -email’s tld which has two characters can not contains digit
Valid:
mkyong@yahoo.com
mkyong-100@yahoo.com
mkyong.100@yahoo.com
mkyong111@mkyong.com
mkyong-100@mkyong.net
mkyong.100@mkyong.com.au
mkyong@1.com
mkyong@gmail.com.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})+)$
Updated:
^([\w-]|(?<!\.)\.)+[a-zA-Z0-9]@[a-zA-Z0-9]([a-zA-Z0-9\-]+)((\.([a-zA-Z]){2,9}){0,2})$
这篇关于正则表达式:电子邮件验证,仅允许域和顶级域中间的连字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!