多年来,我慢慢地开发了一个正则表达式,它可以正确验证大多数电子邮件地址,假设它们不使用IP地址作为服务器部分。
我在几个PHP程序中使用它,而且大多数时候都可以使用。然而,不时有人与我联系,他们对使用它的网站有问题,我最终不得不做出一些调整(最近我意识到我不允许四个字符的TLD)。
你有或见过验证电子邮件的最佳正则表达式是什么?
我见过几种使用函数的解决方案,这些函数使用了几个较短的表达式,但我宁愿在简单函数中使用一个长的复杂表达式,而不是在更复杂的函数中使用几个短表达式。
多年来,我慢慢地开发了一个正则表达式,它可以正确验证大多数电子邮件地址,假设它们不使用IP地址作为服务器部分。
我在几个PHP程序中使用它,而且大多数时候都可以使用。然而,不时有人与我联系,他们对使用它的网站有问题,我最终不得不做出一些调整(最近我意识到我不允许四个字符的TLD)。
你有或见过验证电子邮件的最佳正则表达式是什么?
我见过几种使用函数的解决方案,这些函数使用了几个较短的表达式,但我宁愿在简单函数中使用一个长的复杂表达式,而不是在更复杂的函数中使用几个短表达式。
当前回答
这是我做的。它不是防弹版,但它“简单”,几乎可以检查所有内容。
[\w+-]+(?:\.[\w+-]+)*@[\w+-]+(?:\.[\w+-]+)*(?:\.[a-zA-Z]{2,4})
我认为已经有了解释,因此您可以根据需要进行修改:
(e) [\w+-]+匹配a-z、a-z、_、+、-至少一次
(m) (?:\.[\w+-]+)*匹配a-z、a-z、_、+、-0或更多次,但需要以a开头。(点)
@ = @
(i) [\w+-]+匹配a-z、a-z、_、+、-至少一次
(l) (?:\.[\w+-]+)*匹配a-z、a-z、_、+、-0或更多次,但需要以a开头。(点)
(com)(?:\.[a-zA-Z]{2,4})匹配a-Z,a-Z 2到4次,从a开始。(点)
给出e(-m)@i(.l).com,其中(.m)和(.l)是可选的,但也可以重复多次。
我认为这会验证所有有效的电子邮件地址,但会阻止潜在的无效地址,而不使用在大多数情况下不需要的过于复杂的正则表达式。
请注意,这将允许+@-.com,但这是保持简单的折衷方案。
其他回答
以下是验证电子邮件地址的正则表达式:
^.+@\w+(\.\w+)+$
根据W3C HTML5规范:
^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$
上下文:
有效的电子邮件地址是与ABNF生产[…]匹配的字符串。注意:这一要求故意违反了RFC 5322,RFC 5322为电子邮件地址定义了一种语法,该语法同时太严格(在“@”字符之前)、太模糊(在“@”字符之后)和太松散(允许注释、空白字符和引用字符串以大多数用户不熟悉的方式),在这里无法实际使用。以下JavaScript和Perl兼容的正则表达式是上述定义的实现。/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9]?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
世界上最流行的博客平台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 );
}
根据W3C和Wikipedia的有效正则表达式
[A-Z0-9a-z.!#$%&'*+-/=?^_`{|}~]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}
例如,!#$%&'*+-/=?^_`。{|}~@example.com
列表项目
我使用此功能
function checkmail($value) {
$value = trim($value);
if (stristr($value,"@") &&
stristr($value,".") &&
(strrpos($value, ".") - stripos($value, "@") > 2) &&
(stripos($value, "@") > 1) &&
(strlen($value) - strrpos($value, ".") < 6) &&
(strlen($value) - strrpos($value, ".") > 2) &&
($value == preg_replace('/[ ]/', '', $value)) &&
($value == preg_replace('/[^A-Za-z0-9\-_.@!*]/', '', $value))
)
{
}
else {
return "Invalid Mail-Id";
}
}