C# 电子邮件正则表达式——有遵守 RFC 2822 指南的吗?
我意识到有大量的正则表达式电子邮件验证,但我似乎找不到符合 RFC 2822
标准。
我发现那些不断让[email protected]这样的垃圾邮件通过的邮件。
如果其中一个问题已经按照 RFC 2822 得到了回答(但没有注释),请原谅我。
I realize that there are a ton of regex email validations, but I can't seem to find one that adheres to the RFC 2822
standard.
The ones I find keep letting in junk like [email protected] get through.
Forgive me if the one of the questions is already answered adhering to RFC 2822 (but not annotated that it is).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不久前,我就此发表了一篇文章。是的,可以使用 .NET 正则表达式,因为它们有一个非常规功能,称为“平衡组< /a>”。
经常发布的 Perl RFC822 与电子邮件地址不完全匹配,因为它需要预处理来删除注释。它也适用于非常古老的 RFC(从 1982 年开始!)。
此正则表达式适用于最新的 RFC5322。它还可以正确处理所有注释和折叠空格。
这是正则表达式:
但是,有一些警告。 RFC5322 对域名比实际域 RFC 更加自由,并且各种 RFC 还存在其他限制,例如实际 SMTP RFC 本身(指定最大长度)。因此,即使电子邮件根据 5322 是正确的,但通过各种其他措施也可能是无效的。
黄金测试仍然是向该地址发送一封带有验证码的电子邮件。
I did a post on this a short while ago. Yes, it is possible using .NET regex, since they have a non-regular feature called "balancing groups".
The Perl RFC822 one that is often posted doesn't fully match email addresses, since it requires preprocessing to remove comments. It's also for a very old RFC (from 1982!).
This regex is for RFC5322, which is current. It also handles all comments and folding whitespace correctly.
Here is the regex:
Some caveats, however. RFC5322 is more liberal with domain names than the actual domain RFCs, and there are other restrictions that apply from various RFCs such as the actual SMTP RFC itself (which specifies a maximum length). So even though an email is correct according to 5322 it can be invalid by various other measures.
The golden test is still to send an email to the address with a validation code.
这是针对 RFC822 的,不适用于较新的 RFC822。但地址格式似乎尚未更改,所以应该是你在寻找什么。
(请注意正则表达式下面的注释——它仍然假设该地址已被预处理)
This is for RFC822, not for the newer one. But it seems the address format has not been changed, so should be what you're looking for.
(note the remark below the regexp--it still assumes that the address has been preprocessed)
它在 PCRE 中运行:http://code.iamcal.com/php/rfc822/full_regexp。 txt
显然是 32k。
说真的 - 也许考虑放弃使用单个正则表达式,或接受所有可能的电子邮件表单。
This runs in PCRE: http://code.iamcal.com/php/rfc822/full_regexp.txt
It's 32k, apparently.
Seriously - maybe consider backing off using a single regexp, or accepting ALL possible email forms.
我会看看这个: http://www.regular-expressions.info/email.html,它解释了很多关于使用正则表达式来匹配电子邮件地址的信息,并包含完整的 RFC 2822 表达式,老实说我几乎从不建议使用它。
I would look at this: http://www.regular-expressions.info/email.html, which explains a lot about using regular expressions to match email addresses, and includes a full RFC 2822 expression which honestly I would almost never recommended using.