Ruby Email validation with regex

前端 未结 9 913
有刺的猬
有刺的猬 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: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.

提交回复
热议问题