Modify regex to validate email?

后端 未结 3 1623
感动是毒
感动是毒 2021-01-15 06:21

preg_match(\"/^[-\\w.]+@([A-z0-9][-A-z0-9]+\\.)+[A-z]{2,4}$/\", $email);

I want to modify this regex to allow emails such as user+one@wherever.com

相关标签:
3条回答
  • 2021-01-15 06:57

    Just add the + in the [-\w.] part:

    preg_match("/^[-\w.+]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}$/", $email);
    

    Note that the filter extension has an email validator that can properly validate email addresses:

    if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
        // invalid
    }
    

    See filter_var[doc] and the FILTER_VALIDATE_EMAIL[doc] filter.

    FYI here is the regex used by FILTER_VALIDATE_EMAIL:

    /^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})
    (?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?
    :(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22
    (?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-
    \\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3
    F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5
    D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[
    a-z0-9]+(?:-[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]
    +))(?:-[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(
    ?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f
    0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]
    {1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?:
    :(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?
    :1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(
    ?:[1-9]?[0-9]))){3}))\\]))$/iD
    
    0 讨论(0)
  • 2021-01-15 07:05

    Add + to the first character set:

    preg_match("/^[-\w.+]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}$/", $email);
    
    0 讨论(0)
  • 2021-01-15 07:06

    Just to point out a concern with the previous answers: '+' and '.' cannot be the first character in the username part of the email address. The correct regex should be

    preg_match("/^\w[-\w.+]*@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}$/", $email);
    

    Note that in order to validate against the full RFC 2822, you'll need something like this:

    preg_match('/(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/', $email);
    

    However this is not very practical for most application. Have a look at this page for a detailed discussion of regex validation of email addresses.

    0 讨论(0)
提交回复
热议问题