强悍的电子邮件地址(email address)正则表达式
2011-12-05 19:15
330 查看
1,匹配类似 "lc <deerchao@xxx.com>" 这样的包含姓名的格式.
2,添加了四个命名组: name, email, user, domain. 这样可以方便取出相应的信息.
Regex re = new Regex(@"^((?'name'.+?)\s*<)?(?'email'(?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|""(?'user'(?=[\x01-\x7f])[^""\\]|\\[\x01-\x7f])*""\x20*)*(?'angle'<))?(?'user'(?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|""((?=[\x01-\x7f])[^""\\]|\\[\x01-\x7f])*"")@(?'domain'((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?'angle')(?(name)>)$", RegexOptions.Multiline | RegexOptions.ExplicitCapture);
MatchCollection mc = re.Matches(@"l c <abc@example.com>
Abc@example.com
aBC@example.com
abc.123@example.com
");
foreach (Match ma in mc)
{
}
原表达式的最大优点就是匹配能力强大,能吃下各种符合规范(RFC2882)的表达式.
匹配WikiPedia上的10/11个合法邮件地址格式,不合法的一个也不匹配:
Valid e-mail addresses
abc@example.com
Abc@example.com
aBC@example.com
abc.123@example.com
1234567890@example.com
_______@example.com
abc+mailbox/department=shipping@example.com
!#$%&'*+-/=?^_`.{|}~@example.com (all of these characters are allowed)
"abc@def"@example.com (anything goes inside quotation marks)
"Fred Bloggs"@example.com
"%()[]\;:,<>"@example.com
Invalid e-mail addresses
Abc.example.com (character @ is missing)
Abc.@example.com (character dot(.) is last in local part)
Abc..123@example.com (character dot(.) is double)
A@b@c@example.com (only one @ is allowed outside quotations marks)
%()[]\;:,<>@example.com (none of the characters before the @ is allowed outside quotation marks)
2,添加了四个命名组: name, email, user, domain. 这样可以方便取出相应的信息.
Regex re = new Regex(@"^((?'name'.+?)\s*<)?(?'email'(?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|""(?'user'(?=[\x01-\x7f])[^""\\]|\\[\x01-\x7f])*""\x20*)*(?'angle'<))?(?'user'(?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|""((?=[\x01-\x7f])[^""\\]|\\[\x01-\x7f])*"")@(?'domain'((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?'angle')(?(name)>)$", RegexOptions.Multiline | RegexOptions.ExplicitCapture);
MatchCollection mc = re.Matches(@"l c <abc@example.com>
Abc@example.com
aBC@example.com
abc.123@example.com
");
foreach (Match ma in mc)
{
}
原表达式的最大优点就是匹配能力强大,能吃下各种符合规范(RFC2882)的表达式.
匹配WikiPedia上的10/11个合法邮件地址格式,不合法的一个也不匹配:
Valid e-mail addresses
abc@example.com
Abc@example.com
aBC@example.com
abc.123@example.com
1234567890@example.com
_______@example.com
abc+mailbox/department=shipping@example.com
!#$%&'*+-/=?^_`.{|}~@example.com (all of these characters are allowed)
"abc@def"@example.com (anything goes inside quotation marks)
"Fred Bloggs"@example.com
"%()[]\;:,<>"@example.com
Invalid e-mail addresses
Abc.example.com (character @ is missing)
Abc.@example.com (character dot(.) is last in local part)
Abc..123@example.com (character dot(.) is double)
A@b@c@example.com (only one @ is allowed outside quotations marks)
%()[]\;:,<>@example.com (none of the characters before the @ is allowed outside quotation marks)
相关文章推荐
- 强悍的电子邮件地址(email address)正则表达式
- 强悍的电子邮件地址(email address)正则表达式
- 电话、地址、Email等常用正则表达式
- 手机、火车飞机时刻、Email 电子邮件地址验证等 WEB 服务
- Email 电子邮件地址验证 WEB 服务
- 正则表达式验证Email地址
- 正则表达式检测用户输入的email地址是否合法
- 正则表达式--抓取email地址
- js正则表达式 验证手机号,email地址和邮政编码
- 已经为另一个apple id 验证电子邮件地址的问题
- 一个非常不错的Email地址校验函数
- 用GetprocAddress得到地址后该如何调用这个函数
- 一个非常不错的Email地址校验函数
- 为什么要使用SocketAddress来管理网络地址
- 前端笔试题笔记:拍扁多维数组、提取URL中的参数变成对象、string的trim()函数、判断Email地址、16进制颜色转rgb
- Validate email address using regular expression in SAP ABAP
- 正则查找一篇文章中的email地址
- linux基础之uboot load address、entry point、 bootm address以及kernel运行地址的意义及联系
- 解决 Github用户名 变为 invalid-email-address 问题
- Email地址,URL地址,电话号码提取规则