电子邮件地址的正则表达式识别难吗? [英] Is regular expression recognition of an email address hard?

查看:42
本文介绍了电子邮件地址的正则表达式识别难吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在某处读到写一个正则表达式来匹配电子邮件地址,考虑到标准的所有变化和可能性是非常困难的,而且比人们最初假设的要复杂得多.

I recently read somewhere that writing a regexp to match an email address, taking into account all the variations and possibilities of the standard is extremely hard and is significantly more complicated than what one would initially assume.

这是为什么?

是否有任何已知且经过验证的正则表达式可以完全做到这一点?

Are there any known and proven regexps that actually do this fully?

使用正则表达式匹配电子邮件地址有哪些好的替代方法?

What are some good alternatives to using regexps for matching email addresses?

推荐答案

对于正式电子邮件规范,是的,由于评论(尤其是如果您不先删除对空白的评论),以及各种不同的格式(电子邮件地址并不总是someone@somewhere.tld).您可以接近(使用一些大量且难以理解的正则表达式模式),但检查电子邮件的更好方法是进行非常熟悉的握手:

For the formal e-mail spec, yes, it is technically impossible via Regex due to the recursion of things like comments (especially if you don't remove comments to whitespace first), and the various different formats (an e-mail address isn't always someone@somewhere.tld). You can get close (with some massive and incomprehensible Regex patterns), but a far better way of checking an e-mail is to do the very familiar handshake:

  • 他们会告诉你他们的电子邮件
  • 您通过电子邮件向他们发送带有 Guid 的确认链接
  • 当他们点击链接时,您知道:

  • they tell you their e-mail
  • you e-mail them a confimation link with a Guid
  • when they click on the link you know that:

  1. 电子邮件正确
  2. 存在
  3. 他们拥有它

比盲目接受电子邮件地址要好得多.

Far better than blindly accepting an e-mail address.

这篇关于电子邮件地址的正则表达式识别难吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆