此RegEx验证电子邮件有什么问题? [英] What's wrong with this RegEx for validating emails?

查看:71
本文介绍了此RegEx验证电子邮件有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是用于验证电子邮件的正则表达式- \S+@\S+\.\S + ,我没有写。我是正则表达式的新手,对它们并不十分了解。

Here's a regex for validating emails - \S+@\S+\.\S+, I didn't write it. I'm new to Regular Expressions and do not understand them all that well.

我有几个问题:


  1. 上述RegEx有什么错误

  2. $ b $有什么 RegEx? b验证电子邮件?

  1. What's wrong with the above RegEx?
  2. What's a good RegEx for validating emails?


推荐答案

如何使用正则表达式验证电子邮件是其中之一正则表达式时会出现更普遍的问题,唯一真正的好答案是您不愿意。在此网站 中对此进行了讨论> 很多 场合。您必须了解的是,如果您真的想遵守该规范,则您的正则表达式将类似于。显然,这是一种怪兽,更是一种练习,说明坚持您应该接受的内容有多么荒谬。考虑到这一点,如果您绝对肯定需要知道该电子邮件地址是有效的,那么检查该地址的唯一真正方法是将一条消息实际发送到该电子邮件地址,然后检查该邮件是否退回。否则,此正则表达式将正确验证大多数情况,并且在很多情况下,大多数情况下足够的。此外,该页面还将讨论尝试使用正则表达式验证电子邮件的问题。

"How do I validate an email with regex" is one of the more popular questions that come up when it comes to regular expressions and the only real good answer is "you don't". It has been discussed in this very website in many occasions. What you have to understand is that if you really wanted to follow the spec, your regex would look something like this. Obviously that is a monstrosity and is more an exercise in demonstrating how ridiculously difficult it is to adhere to what you are supposed to be able to accept. With that in mind, if you absolutely positively need to know that the email address is valid, the only real way to check for that is to actually send a message to the email address and check if it bounces or not. Otherwise, this regex will properly validate most cases, and in a lot of situations most cases is enough. In addition, that page will discuss the problems with trying to validate emails with regex.

这篇关于此RegEx验证电子邮件有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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