多年来,我慢慢地开发了一个正则表达式,它可以正确验证大多数电子邮件地址,假设它们不使用IP地址作为服务器部分。

我在几个PHP程序中使用它,而且大多数时候都可以使用。然而,不时有人与我联系,他们对使用它的网站有问题,我最终不得不做出一些调整(最近我意识到我不允许四个字符的TLD)。

你有或见过验证电子邮件的最佳正则表达式是什么?

我见过几种使用函数的解决方案,这些函数使用了几个较短的表达式,但我宁愿在简单函数中使用一个长的复杂表达式,而不是在更复杂的函数中使用几个短表达式。


当前回答

根据官方标准RFC 2822,有效的电子邮件正则表达式为:

(?:[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])+)\])

如果您想在Java中使用它,这非常简单:

import java.util.regex.*;

class regexSample 
{
   public static void main(String args[]) 
   {
      //Input the string for validation
      String email = "xyz@hotmail.com";

      //Set the email pattern string
      Pattern p = Pattern.compile(" (?:[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])+)\\]");

      //Match the given string with the pattern
      Matcher m = p.matcher(email);

      //Check whether match is found 
      boolean matchFound = m.matches();

      if (matchFound)
        System.out.println("Valid Email Id.");
      else
        System.out.println("Invalid Email Id.");
   }
}

其他回答

奇怪的是,您“不能”使用4个字符的TLD。您禁止人们使用.info和.name,以及长度限制stop、travel和.museum,但是的,它们比2个字符的TLD和3个字符的TLC更常见。

你也应该允许大写字母。电子邮件系统将规范本地部分和域部分。

对于域部分的正则表达式,域名不能以'-'开头,也不能以'-'结尾。Dash只能介于两者之间。

如果您使用PEAR库,请查看他们的邮件功能(我忘记了确切的名称/库)。您可以通过调用一个函数来验证电子邮件地址,它根据RFC 822中的定义验证电子邮件地址。

世界上最流行的博客平台WordPress使用此功能验证电子邮件地址。。。

但他们是通过多个步骤来实现的。

使用此函数中提到的正则表达式时,您不必再担心。。。

这是函数。。。

/**
 * Verifies that an email is valid.
 *
 * Does not grok i18n domains. Not RFC compliant.
 *
 * @since 0.71
 *
 * @param string $email Email address to verify.
 * @param boolean $deprecated Deprecated.
 * @return string|bool Either false or the valid email address.
 */
function is_email( $email, $deprecated = false ) {
    if ( ! empty( $deprecated ) )
        _deprecated_argument( __FUNCTION__, '3.0' );

    // Test for the minimum length the email can be
    if ( strlen( $email ) < 3 ) {
        return apply_filters( 'is_email', false, $email, 'email_too_short' );
    }

    // Test for an @ character after the first position
    if ( strpos( $email, '@', 1 ) === false ) {
        return apply_filters( 'is_email', false, $email, 'email_no_at' );
    }

    // Split out the local and domain parts
    list( $local, $domain ) = explode( '@', $email, 2 );

    // LOCAL PART
    // Test for invalid characters
    if ( !preg_match( '/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/', $local ) ) {
        return apply_filters( 'is_email', false, $email, 'local_invalid_chars' );
    }

    // DOMAIN PART
    // Test for sequences of periods
    if ( preg_match( '/\.{2,}/', $domain ) ) {
        return apply_filters( 'is_email', false, $email, 'domain_period_sequence' );
    }

    // Test for leading and trailing periods and whitespace
    if ( trim( $domain, " \t\n\r\0\x0B." ) !== $domain ) {
        return apply_filters( 'is_email', false, $email, 'domain_period_limits' );
    }

    // Split the domain into subs
    $subs = explode( '.', $domain );

    // Assume the domain will have at least two subs
    if ( 2 > count( $subs ) ) {
        return apply_filters( 'is_email', false, $email, 'domain_no_periods' );
    }

    // Loop through each sub
    foreach ( $subs as $sub ) {
        // Test for leading and trailing hyphens and whitespace
        if ( trim( $sub, " \t\n\r\0\x0B-" ) !== $sub ) {
            return apply_filters( 'is_email', false, $email, 'sub_hyphen_limits' );
        }

        // Test for invalid characters
        if ( !preg_match('/^[a-z0-9-]+$/i', $sub ) ) {
            return apply_filters( 'is_email', false, $email, 'sub_invalid_chars' );
        }
    }

    // Congratulations your email made it!
    return apply_filters( 'is_email', $email, $email, null );
}

为这个问题发布的正则表达式现在已经过时了,因为新的通用顶级域(gTLD)即将到来(例如,伦敦、篮球、。通販). 要验证电子邮件地址,有两个答案(这将与绝大多数人相关)。

正如主要答案所说,不要使用正则表达式。只需通过向该地址发送电子邮件来验证它(捕获无效地址的异常)使用一个非常通用的正则表达式,至少确保他们使用的是一个类似于{something}@{some}的电子邮件结构。{一些}。使用一个详细的正则表达式是没有意义的,因为你不会全部抓住它们,几年后会有一个新的批处理,你必须再次更新正则表达式。

我决定使用正则表达式,因为不幸的是,有些用户没有读取表单,并将错误的数据放在错误的字段中。这至少会在他们试图将非电子邮件的内容输入到电子邮件输入字段时提醒他们,这样可以为您节省一些时间来支持用户处理电子邮件问题。

(.+)@(.+){2,}\.(.+){2,}

没有一个是真正有用的。我在回答“是否有用于电子邮件地址验证的PHP库?”时讨论了一些问题?,它也在电子邮件地址的正则表达式识别难吗?中讨论?。

简而言之,不要期望一个可用的正则表达式能够完成正确的工作。最好的正则表达式将验证语法,而不是电子邮件的有效性(jhohn@example.com是正确的,但它可能会反弹…)。

当您使用PHP编写时,我建议您使用PHP内置的电子邮件验证。

filter_var($value, FILTER_VALIDATE_EMAIL)

如果您运行的是低于5.3.6的PHP版本,请注意这个问题:错误#53091:当我试图过滤一个大于2264个字符的文本时出现崩溃

如果您想了解此内置验证如何工作的更多信息,请参阅此处:PHP的filter_var filter_VALIDATE_EMAIL实际工作吗?