-4

Found this technique to verify if the email address is in correct form of not.

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;
        }       
    }

I Am a newbie in php. What this big regex commands mean?

Hassan Rafi
  • 1
  • 1
  • 4
  • 1
    I suggest looking at regex tools online, you can paste the regex in and it will explain each step, such as https://regexr.com/ – Jeff Atwood Mar 18 '18 at 08:28
  • I did at regex101 it matches. But I am confused at "(\[?)" is it check that is there any [ after @ or what? And the ^ at the beginning and $ at the end before php delimiter what does that stand for? – Hassan Rafi Mar 18 '18 at 08:34
  • These patterns are a mess. I advise that you discontinue looking at this snippet, and start searching SO for some good email validating regex ...or even better validate the email address without regex -- look for: `filter_var($email, FILTER_VALIDATE_EMAIL)`. – mickmackusa Mar 18 '18 at 08:52

1 Answers1

1

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)
mickmackusa
  • 33,121
  • 11
  • 58
  • 86
  • Please explain the downvote. Is something I've said ill-advised or incorrect? What could be the benefit of downvoting a correct / informative answer? If you don't like the question, downvote the question. If there is something wrong with my answer that merits a downvote, I'd like to hear it. – mickmackusa Mar 18 '18 at 21:11
  • I did the question and I find your answer useful sir. Now some people are saying possible duplication. But don't think they understand some times those articles are not useful enough to explain a specific regex command. I have tried. But all finger in a hand not equal u know. I am a new user in stack so I can not up vote. But thank u so much for your effort mickmackusa. But I have one more qus. What is the purpose of a capture group? and Any email does have [ or ] this sign, right? – Hassan Rafi Mar 19 '18 at 06:55
  • @Hassan without more rep points, your only way to reward a helpful answer is to award it the big green tick (these can only be awarded to one answer pet page). I believe the reason your question has been closed is because other volunteers felt your question was under-researched. The capture groups in both patterns are used to isolate certain portions of the pattern and avoid unintended inaccuracies with the alternatives (`|`). Your task is about validation (not extraction) so the capture groups don't create a bloated output with subpattern matches. – mickmackusa Mar 19 '18 at 10:07
  • Offhand, I don't know if square brackets are valid characters in an email. I'm pretty sure that I've never seen an email containing square brackets. I urge you to use an email validating function rather than regex. – mickmackusa Mar 19 '18 at 10:09
  • When I am designing and testing regex patterns, I go to www.regex101.com it is a beautiful tool with great features that are effective and educational. – mickmackusa Mar 19 '18 at 10:10