根据RFC5322和https://en.wikipedia.org/wiki/Email_address的电子邮件ID验证 [英] Email Id validation according to RFC5322 and https://en.wikipedia.org/wiki/Email_address
问题描述
根据 RFC5322 并遵循
https://en.wikipedia.org/wiki/Email_address
下面是使用Java的示例代码
Below is the sample code using java and a regular expression to validate E-mail Ids.
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模式无效。
How can I change my regular expression so that it will invalidate the below patterns of email ids.
1234567890123456789012345678901234567890123456789012345678901234+x@example.com
john..doe@example.com
john.doe@example..com
just"not"right@example.com
以下是正则表达式的标准:
Below are the criteria for regular expression:
本地部分
电子邮件的本地部分地址可以使用以下任何ASCII字符:
The local-part of the email address may use any of these ASCII characters:
- 大写和小写拉丁字母
A到Z
和a到z
; - 数字
0到9
; - 特殊字符!#$%&'* +-/ =?^ _`{|}〜
- 点
。
,条件是除非加引号
,否则不是第一个或最后一个字符,并且除非加引号,否则它不会连续出现
(例如不允许John..Doe @ example.com
,但
John..Doe @ example.com
); -
空格
和(),:;<&@@ [ \]
字符,但有限制
(它们只能在带引号的字符串内使用,如下面的
段落所述,此外,反斜杠或双引号必须在
之前加上反斜杠);在本地部分的任一端都可以在括号中加上括号
;例如
john.smith(comment)@example。 com
和
(评论)john.smith@example.com
都等同于
john.smith@example.com
。
- uppercase and lowercase Latin letters
A to Z
anda to z
; - digits
0 to 9
; - special characters !#$%&'*+-/=?^_`{|}~
- dot
.
, provided that it is not the first or last character unless quoted, and provided also that it does not appear consecutively unless quoted (e.g.John..Doe@example.com
is not allowed but"John..Doe"@example.com
is allowed); space
and"(),:;<>@[\]
characters are allowed with restrictions (they are only allowed inside a quoted string, as described in the paragraph below, and in addition, a backslash or double-quote must be preceded by a backslash); comments are allowed with parentheses at either end of the local-part; e.g.john.smith(comment)@example.com
and(comment)john.smith@example.com
are both equivalent tojohn.smith@example.com
.
域
- 大写和小写拉丁字母
A到Z
和a到z
; - 位数
0到9
,前提是顶级域名不是
全部-numeric; - 连字符
-
,只要它不是第一个或最后一个ch拖拉机。
在域以及本地部分都允许注释;例如
,john.smith @(comment)example.com
和
john.smith@example.com(comment )
等同于
john.smith@example.com
。
- uppercase and lowercase Latin letters
A to Z
anda to z
; - digits
0 to 9
, provided that top-level domain names are not all-numeric; - hyphen
-
, provided that it is not the first or last character. Comments are allowed in the domain as well as in the local-part; for example,john.smith@(comment)example.com
andjohn.smith@example.com(comment)
are equivalent tojohn.smith@example.com
.
推荐答案
您可以像这样 RFC5322
(参考正则表达式已修改)
"(?im)^(?=.{1,64}@)(?:(\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"@)|((?:[0-9a-z](?:\\.(?!\\.)|[-!#\\$%&'\\*\\+/=\\?\\^`\\{\\}\\|~\\w])*)?[0-9a-z]@))(?=.{1,255}$)(?:(\\[(?:\\d{1,3}\\.){3}\\d{1,3}\\])|((?:(?=.{1,63}\\.)[0-9a-z][-\\w]*[0-9a-z]*\\.)+[a-z0-9][\\-a-z0-9]{0,22}[a-z0-9])|((?=.{1,63}$)[0-9a-z][-\\w]*))$"
https://regex101.com/r/ObS3QZ/1
# (?im)^(?=.{1,64}@)(?:("[^"\\]*(?:\\.[^"\\]*)*"@)|((?:[0-9a-z](?:\.(?!\.)|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)?[0-9a-z]@))(?=.{1,255}$)(?:(\[(?:\d{1,3}\.){3}\d{1,3}\])|((?:(?=.{1,63}\.)[0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9])|((?=.{1,63}$)[0-9a-z][-\w]*))$
# Note - remove all comments '(comments)' before runninig this regex
# Find \([^)]*\) replace with nothing
(?im) # Case insensitive
^ # BOS
# Local part
(?= .{1,64} @ ) # 64 max chars
(?:
( # (1 start), Quoted
" [^"\\]*
(?: \\ . [^"\\]* )*
"
@
) # (1 end)
| # or,
( # (2 start), Non-quoted
(?:
[0-9a-z]
(?:
\.
(?! \. )
| # or,
[-!#\$%&'\*\+/=\?\^`\{\}\|~\w]
)*
)?
[0-9a-z]
@
) # (2 end)
)
# Domain part
(?= .{1,255} $ ) # 255 max chars
(?:
( # (3 start), IP
\[
(?: \d{1,3} \. ){3}
\d{1,3} \]
) # (3 end)
| # or,
( # (4 start), Others
(?: # Labels (63 max chars each)
(?= .{1,63} \. )
[0-9a-z] [-\w]* [0-9a-z]*
\.
)+
[a-z0-9] [\-a-z0-9]{0,22} [a-z0-9]
) # (4 end)
| # or,
( # (5 start), Localdomain
(?= .{1,63} $ )
[0-9a-z] [-\w]*
) # (5 end)
)
$ # EOS
如何使sudhansu_@gmail.com成为有效的电子邮件ID – Mihir 2月7日,9:34
我认为规范希望将本地部分用引号引起来
或用 [0-9a-z]包裹
。
I think the spec wants the local part to be either encased in quotes
or, to be encased by [0-9a-z]
.
但是,为了避开稍后并使 sudhansu_@gmail.com
有效,只需
将此组2替换为:
But, to get around the later and make sudhansu_@gmail.com
valid, just
replace group 2 with this:
( # (2 start), Non-quoted
[0-9a-z]
(?:
\.
(?! \. )
| # or,
[-!#\$%&'\*\+/=\?\^`\{\}\|~\w]
)*
@
) # (2 end)
新正则表达式
"(?im)^(?=.{1,64}@)(?:(\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"@)|([0-9a-z](?:\\.(?!\\.)|[-!#\\$%&'\\*\\+/=\\?\\^`\\{\\}\\|~\\w])*@))(?=.{1,255}$)(?:(\\[(?:\\d{1,3}\\.){3}\\d{1,3}\\])|((?:(?=.{1,63}\\.)[0-9a-z][-\\w]*[0-9a-z]*\\.)+[a-z0-9][\\-a-z0-9]{0,22}[a-z0-9])|((?=.{1,63}$)[0-9a-z][-\\w]*))$"
新演示
https ://regex101.com/r/ObS3QZ/5
这篇关于根据RFC5322和https://en.wikipedia.org/wiki/Email_address的电子邮件ID验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!