强悍的电子邮件地址(email address)正则表达式

原始来源: RegexLib.com
修改后加入了两个新功能:
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)

posted on   deerchao  阅读(5049)  评论(1编辑  收藏  举报

编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
< 2008年6月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 1 2 3 4 5
6 7 8 9 10 11 12

统计

点击右上角即可分享
微信分享提示