What's wrong with this RegEx for validating emails?

后端 未结 5 1684
轻奢々
轻奢々 2020-12-11 04:40

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.

5条回答
  •  没有蜡笔的小新
    2020-12-11 04:43

    "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.

提交回复
热议问题