正则表达式是一个正则表达式:它是一种描述一组字符串的模式,通常是所有可能字符串的一个子集。正则表达式可以使用的所有特殊字符都在您的问题被标记为重复的问题中进行了说明。
但专门针对您的情况;有一个很好的工具可以解释正则表达式here:
NODE EXPLANATION
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
[\w.-]+ any character of: word characters (a-z, A-
Z, 0-9, _), '.', '-' (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
@ '@'
--------------------------------------------------------------------------------
[\w.-]+ any character of: word characters (a-z, A-
Z, 0-9, _), '.', '-' (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
[A-Za-z]{2,6} any character of: 'A' to 'Z', 'a' to 'z'
(between 2 and 6 times (matching the most
amount possible))
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
验证电子邮件地址的正确方法
但是,如果您使用的是 PHP >= 5.2.0(您可能是),则不需要为此使用正则表达式。使用内置的filter_var(),代码更清晰:
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// email valid
} else {
// email invalid
}
您不必担心边界情况或其他任何事情。