gpt4 book ai didi

java - 根据 RFC5322 和 https ://en. 进行电子邮件 ID 验证 wikipedia.org/wiki/Email_address

转载 作者:行者123 更新时间:2023-12-01 07:00:52 25 4
gpt4 key购买 nike

根据 RFC5322 和以下规则验证电子邮件 ID

https://en.wikipedia.org/wiki/Email_address

下面是使用 java 和正则表达式来验证电子邮件 ID 的示例代码。

public void checkValid() {
List<String> emails = new ArrayList();
//Valid Email Ids
emails.add("simple@example.com");
emails.add("very.common@example.com");
emails.add("disposable.style.email.with+symbol@example.com");
emails.add("other.email-with-hyphen@example.com");
emails.add("fully-qualified-domain@example.com");
emails.add("user.name+tag+sorting@example.com");
emails.add("fully-qualified-domain@example.com");
emails.add("x@example.com");
emails.add("carlosd'intino@arnet.com.ar");
emails.add("example-indeed@strange-example.com");
emails.add("admin@mailserver1");
emails.add("example@s.example");
emails.add("\" \"@example.org");
emails.add("\"john..doe\"@example.org");

//Invalid emails Ids
emails.add("Abc.example.com");
emails.add("A@b@c@example.com");
emails.add("a\"b(c)d,e:f;g<h>i[j\\k]l@example.com");
emails.add("just\"not\"right@example.com");
emails.add("this is\"not\\allowed@example.com");
emails.add("this\\ still\"not\\allowed@example.com");
emails.add("1234567890123456789012345678901234567890123456789012345678901234+x@example.com");
emails.add("john..doe@example.com");
emails.add("john.doe@example..com");

String regex = "^[a-zA-Z0-9_!#$%&'*+/=? \\\"`{|}~^.-]+@[a-zA-Z0-9.-]+$";

Pattern pattern = Pattern.compile(regex);
int i=0;
for(String email : emails){
Matcher matcher = pattern.matcher(email);
System.out.println(++i +"."+email +" : "+ matcher.matches());
}
}

实际输出:

   1.simple@example.com : true
2.very.common@example.com : true
3.disposable.style.email.with+symbol@example.com : true
4.other.email-with-hyphen@example.com : true
5.fully-qualified-domain@example.com : true
6.user.name+tag+sorting@example.com : true
7.fully-qualified-domain@example.com : true
8.x@example.com : true
9.carlosd'intino@arnet.com.ar : true
10.example-indeed@strange-example.com : true
11.admin@mailserver1 : true
12.example@s.example : true
13." "@example.org : true
14."john..doe"@example.org : true
15.Abc.example.com : false
16.A@b@c@example.com : false
17.a"b(c)d,e:f;g<h>i[j\k]l@example.com : false
18.just"not"right@example.com : true
19.this is"not\allowed@example.com : false
20.this\ still"not\allowed@example.com : false
21.1234567890123456789012345678901234567890123456789012345678901234+x@example.com : true
22.john..doe@example.com : true
23.john.doe@example..com : true

预期输出:

1.simple@example.com : true
2.very.common@example.com : true
3.disposable.style.email.with+symbol@example.com : true
4.other.email-with-hyphen@example.com : true
5.fully-qualified-domain@example.com : true
6.user.name+tag+sorting@example.com : true
7.fully-qualified-domain@example.com : true
8.x@example.com : true
9.carlosd'intino@arnet.com.ar : true
10.example-indeed@strange-example.com : true
11.admin@mailserver1 : true
12.example@s.example : true
13." "@example.org : true
14."john..doe"@example.org : true
15.Abc.example.com : false
16.A@b@c@example.com : false
17.a"b(c)d,e:f;g<h>i[j\k]l@example.com : false
18.just"not"right@example.com : false
19.this is"not\allowed@example.com : false
20.this\ still"not\allowed@example.com : false
21.1234567890123456789012345678901234567890123456789012345678901234+x@example.com : false
22.john..doe@example.com : false
23.john.doe@example..com : false

如何更改我的正则表达式,使其使以下电子邮件 ID 模式无效。

1234567890123456789012345678901234567890123456789012345678901234+x@example.com
john..doe@example.com
john.doe@example..com
just"not"right@example.com

下面是正则表达式的标准:

本地部分

电子邮件地址的本地部分可以使用以下任何 ASCII 字符:

  1. 大写和小写拉丁字母 A to Za to z
  2. 数字 0 to 9
  3. 特殊字符!#$%&'*+-/=?^_`{|}~
  4. . ,前提是它不是第一个或最后一个字符,除非引用,并且还必须不连续出现除非引用(例如 John..Doe@example.com 是不允许的,但 允许 "John..Doe"@example.com);
  5. 允许使用 space"(),:;<>@[\] 字符,但有限制(它们只允许在带引号的字符串内,如下面的段落,此外,必须使用反斜杠或双引号前面有一个反斜杠);允许带括号的注释在本地部分的任一端;例如 john.smith(comment)@example.com(comment)john.smith@example.com 都相当于 john.smith@example.com

域名

  1. 大写和小写拉丁字母 A to Za to z
  2. 数字 0 to 9 ,前提是顶级域名不是全数字;
  3. 连字符 - ,前提是它不是第一个或最后一个字符。域和本地部分都允许发表评论;为了例如,john.smith@(comment)example.comjohn.smith@example.com(comment) 相当于 john.smith@example.com

最佳答案

这不是你问的问题,但为什么要重新发明轮子?

Apache commons has a class that covers this already .

org.apache.commons.validator.routines.EmailValidator.getInstance().isValid(email)

这样您就无需负责跟上不断变化的电子邮件格式标准。

关于java - 根据 RFC5322 和 https ://en. 进行电子邮件 ID 验证 wikipedia.org/wiki/Email_address,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53299385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com