Ruby-on-rails rails - 用于电子邮件验证的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4770133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:10:18  来源:igfitidea点击:

rails - REGEX for email validation

ruby-on-railsregexruby-on-rails-3

提问by AnApprentice

I'm looking for a regex to validate an email to learn if it's valid or not.. I have the following:

我正在寻找一个正则表达式来验证电子邮件以了解它是否有效..我有以下内容:

def is_a_valid_email?(email)
    email_regex = %r{
      ^ # Start of string
      [0-9a-z] # First character
      [0-9a-z.+]+ # Middle characters
      [0-9a-z] # Last character
      @ # Separating @ character
      [0-9a-z] # Domain name begin
      [0-9a-z.-]+ # Domain name middle
      [0-9a-z] # Domain name end
      $ # End of string
    }xi # Case insensitive

    (email =~ email_regex)
end

Problem with the above is [email protected]does not return as valid when it should be. Any thoughts or suggestions for a better regex?

上面的问题是[email protected]在它应该有效的时候没有返回。关于更好的正则表达式的任何想法或建议?

Thanks

谢谢

采纳答案by jensgram

My shot at this (see comment and link above):

我对此的看法(见上面的评论和链接):

^.+@.+$

回答by scragz

validates_format_of :email, 
  :with => /^(|(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6})$/i

Don't ask me to explain it! That was from a validation plugin that I've since lost track of as all I needed was the email regex.

别问我解释!那是来自一个验证插件,我已经忘记了,因为我需要的只是电子邮件正则表达式。

回答by Sam Thornton

@jensgram's answer is really good, but it actually still allows for an email without a domain, e.g. foo@bar is as valid as [email protected].

@jensgram 的回答非常好,但它实际上仍然允许没有域的电子邮件,例如 foo@bar 与 [email protected] 一样有效。

Here's a slight variation on it that requires [email protected]:

这里有一个需要 [email protected] 的轻微变化:

/\A\S+@.+\.\S+\z/

/\A\S+@.+\.\S+\z/

or, more readable (though the parentheses aren't needed):

或者,更具可读性(尽管不需要括号):

/\A(\S+)@(.+)\.(\S+)\z/

/\A(\S+)@(.+)\.(\S+)\z/

Play around with this

玩这个

*Note: this regex is better than many, more complex ones, because of how incredibly diverse an email is allowed to be. Technically, an email can even use spaces if wrapped in quotes, e.g. "This is a valid email"@email.com

*注意:此正则表达式比许多更复杂的正则表达式要好,因为允许电子邮件具有惊人的多样性。从技术上讲,如果用引号括起来,电子邮件甚至可以使用空格,例如“这是一封有效的电子邮件”@email.com

*Edit: I've replaced the ^and $with \Aand \zto prevent a multiline anchor error in rails testing.

*编辑:我已经更换了^,并$\A\z防止轨测试多锚错误。

*Edit: Thanks to @Barry for noticing that the regex allowed white spaces. Updated the regex with \S to prevent emails with improper whitespaces. Multiple dots are still allowed as [email protected] is an acceptable address.

*编辑:感谢@Barry 注意到正则表达式允许空格。用 \S 更新正则表达式以防止带有不正确空格的电子邮件。仍然允许使用多个点,因为 [email protected] 是可接受的地址。

回答by apanzerj

Not enough reputation to add a comment but if you are going to use the Devise regexp, and you are already using Devise itself, then you can simply use:

没有足够的声誉来添加评论,但如果您打算使用 Devise 正则表达式,并且您已经在使用 Devise 本身,那么您可以简单地使用:

validates :email, presence: true, format: Devise.email_regexp

validates :email, presence: true, format: Devise.email_regexp

回答by Mblake

/\b[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/

This is what I use to validate email.

这是我用来验证电子邮件的。

Source: http://www.regular-expressions.info/email.html

来源:http: //www.regular-expressions.info/email.html

回答by Mblake

Most of these other answers are too restrictive or allow addresses that are clearly wrong. So it's better to give up on being too strict, which will frustrate users and just get the main things right.

这些其他答案中的大多数都过于严格或允许地址明显错误。所以最好放弃过于严格,这会让用户感到沮丧,并把主要的事情做好。

1. The most common use-case:

1. 最常见的用例:

    validates :email, ..., format: { with: /\A[^@\s]+@([^@.\s]+\.)+[^@.\s]+\z/ }

2. To also allow root@localhostor homer@blinky, use this:

2. 也允许root@localhosthomer@blinky,使用这个:

    validates :email, ..., format: { with: /\A[^@\s]+@([^@.\s]+\.)*[^@.\s]+\z/ }

None of the above replaces looking for CAPTCHAs, dns records, dns domain blacklists, IP blacklists and confirmation emails.

以上都不能替代查找 CAPTCHA、dns 记录、dns 域黑名单、IP 黑名单和确认电子邮件。

For real email domain validation, this can be used instead: https://gist.github.com/9200104

对于真正的电子邮件域验证,可以改用:https: //gist.github.com/9200104

This horse is thoroughly beaten now, and being made into kibble.

这匹马现在被彻底打败,被做成粗磨。

回答by Blair Anderson

use: /\A[^@\s]+@[^@\s]+\z/

用: /\A[^@\s]+@[^@\s]+\z/

It asserts that there are no @ symbols or whitespaces in either the localpart or the domain, and that there is a single @ symbol separating the localpart and the domain.

它断言在 localpart 或域中没有 @ 符号或空格,并且有一个 @ 符号分隔 localpart 和域。

More importantly: require email verification

更重要的是:需要电子邮件验证

As more tlds are being created, this is forward thinking.

随着越来越多的 tld 被创建,这是前瞻性的思考。

This is from Devise, an industry standard for authentication.

这是来自Devise,一种用于身份验证的行业标准。

回答by Buniowy

^.+@[\w]+\S[^\s]+$

^.+@[\w]+\S[^\s]+$

A short improvement to first answer

对第一个答案的简短改进

回答by ryanprayogo

Here's an email regex that I've been using for a while:

这是我使用了一段时间的电子邮件正则表达式:

\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

It's from here

它来自这里

回答by Swapnil Chincholkar

For email validation, I come up with this regular expression,

对于电子邮件验证,我想出了这个正则表达式,

/^[\w\d]+@[\w\d]+(\.[\w\d]+)+$/

Don't know how much it will help you but working fine for me.

不知道它对你有多大帮助,但对我来说很好。

If anyone find it failing for any email address please reply me so I will be able to work more on this.

如果有人发现任何电子邮件地址失败,请回复我,以便我能够在此方面进行更多工作。