Ruby Email validation with regex

前端 未结 9 871
有刺的猬
有刺的猬 2020-12-03 00:21

I have a large list of emails I am running through. A lot of the emails have typos. I am trying to build a string that will check valid emails.

this is what I have

相关标签:
9条回答
  • 2020-12-03 00:48

    I guess the example from the book can be improved to match emails with - in subdomain.

    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
    

    For example:

    > 'some@email.with-subdomain.com' =~ VALID_EMAIL_REGEX
    => 0
    
    0 讨论(0)
  • 2020-12-03 00:53

    try this!!!

    /\[A-Z0-9._%+-\]+@\[A-Z0-9.-\]+\.\[AZ\]{2,4}/i

    only email string selected

    "Robert Donhan" <bob@email.com>sadfadf
    Robert Donhan <bob@email.com>
    "Robert Donhan" abc.bob@email.comasdfadf
    Robert Donhan bob@email.comadfd
    
    0 讨论(0)
  • 2020-12-03 00:57

    Yours is complicated indeed.

    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    

    The above code should suffice.

    Explanation of each piece of the expression above for clarification:

    Start of regex:

    /
    

    Match the start of a string:

    \A
    

    At least one word character, plus, hyphen, or dot:

    [\w+\-.]+
    

    A literal "at sign":

    @
    

    A literal dot:

    \.
    

    At least one letter:

    [a-z]+
    

    Match the end of a string:

    \z
    

    End of regex:

    /
    

    Case insensitive:

    i
    

    Putting it back together again:

    /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    

    Check out Rubular to conveniently test your expressions as you write them.

    0 讨论(0)
  • 2020-12-03 00:57

    This works good for me:

    if email.match?('[a-z0-9]+[_a-z0-9\.-]*[a-z0-9]+@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})')
          puts 'matches!'
    else
          puts 'it doesn\'t match!'
    end
    
    0 讨论(0)
  • 2020-12-03 00:59

    Here's a great article by David Celis explaining why every single regular expression you can find for validating email addresses is wrong, including the ones above posted by Mike.

    From the article:

    The local string (the part of the email address that comes before the @) can contain the following characters:

        `! $ & * - = ` ^ | ~ # % ' + / ? _ { }` 
    

    But guess what? You can use pretty much any character you want if you escape it by surrounding it in quotes. For example, "Look at all these spaces!"@example.com is a valid email address. Nice.

    If you need to do a basic check, the best regular expression is simply /@/.

    0 讨论(0)
  • 2020-12-03 01:09

    This has been built into the standard library since at least 2.2.1

    URI::MailTo::EMAIL_REGEXP
    
    0 讨论(0)
提交回复
热议问题