这个正则表达式意味着什么 "/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0 -9]{1,3})(\]?)$/"

What does this regular expression maens "/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/"

发现了这种技术来验证电子邮件地址的格式是否正确。

function check_email($email) {  
        if( (preg_match('/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/', $email)) || 
            (preg_match('/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/',$email)) ) { 
             return true;
        } else {
             return false;
        }       
    }

我是 php 的新手。这个大正则表达式命令是什么意思?

What this big regex commands mean?

模式 #1 细分:

/           #start of pattern delimiter
(@.*@)      #Capture Group #1: match an @ sign, then zero or more (as many as possible) of any non-newline character, then another @ sign
|           #or
(\.\.)      #Capture Group #2: match a literal dot, then another literal dot
|           #or
(@\.)       #Capture Group #3: match an @ sign, then a literal dot
|           #or
(\.@)       #Capture Group #4: match a literal dot, then an @ sign
|           #or
(^\.)       #Capture Group #5: match the start of the string, then a literal dot
/           #end of pattern delimiter

在我看来,第一个模式看起来完全是无用的垃圾。

模式 2 细分:

/                   #start of pattern delimiter
^                   #match start of string
.+                  #match any non-newline character one or more times (as much as possible)
\@                  #match @ (the \ is an escaping character which is not necessary)
(\[?)               #Capture Group #1: match an opening square bracket zero or one time
[a-zA-Z0-9\-\.]+    #match one or more (as much as possible) of the following characters: lowercase letters, uppercase letters, digits, hyphens, and dots (the \ before the . is an escaping character which is not necessary)
\.                  #match a literal dot
(                   #start Capture Group #2
  [a-zA-Z]{2,4}     #match any uppercase or lowercase letter 2, 3, or 4 times
  |                 #or
  [0-9]{1,3}        #match any digit 1, 2, or 3 times
)                   #end Capture Group #2
(\]?)               #Capture Group #3: match a closing square bracket zero or one time
$                   #match the end of the string
/                   #end of pattern delimiter

我不推荐这些模式。

如果您想验证电子邮件,Whosebug 周围有更好的模式,或者您可以使用 filter_var() 调用。

研究这个字符串:

filter_var($email, FILTER_VALIDATE_EMAIL)