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


What this big regex commands mean?

Pattern #1 Breakdown:

/           #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

In my opinion, the first pattern looks like absolute useless rubbish.

Pattern 2 Breakdown:

/                   #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

I would not recommend these patterns.

If you want to validate an email, there are better pattern floating around StackOverflow or you can use a filter_var() call.

Research this string:

filter_var($email, FILTER_VALIDATE_EMAIL)

2

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