I'm using a function that checks if an e-mail address is of a valid format eg name@domain.com.

It uses the following to check if its in a valid format.

(!preg_match("/.+@([?) [a-zA-Z0-9-.]+.([a-zA-Z]{2,3}|[0-9]{1,3})(]?)$/", $email))

I got this code from a book but it dosn't explain it too well and I can't really work out whats happening using the manual so I was hoping someone on here might be able to break it down and point out what exactly it does.

Thanks

    / => starting delimiter
    Here's what I think it should be, when broken down:

    ^ => assert start of subject (i.e. pattern matched must start here)
    .+ => match 1 or more of any character
    @ => literal '@'
    ([?) => subpattern that matches 0 or 1 of '['
    [a-zA-Z0-9-.]+ => match one or more alphanumeric characters, '-' and '.'
    . => literal '.'
    ( => start subpattern
    [a-zA-Z]{2,3} => match 2 or 3 alphabetic characters
    | => or
    [0-9]{1,3} => match 1 to 3 numeric characters
    ) => end subpattern
    (]?) => subpattern that matches 0 or 1 of ']'
    $ => assert end of subject (i.e. pattern matched must end here)
    / => closing delimiter

    These are some examples that I think it should match:
    test@example.com
    1test2@example.org
    test@[example.com]
    test@example.net.com
    test@-example.co
    test@example-.co.uk
    test@.example.123
    test@[example.1
    test@example.12]

    These are some examples that I think it shouldnt match:
    example
    test@example
    test@example.
    test@example.a
    test@example.html
    test@example.1234

    Note that I'm not particularly good at regex, so I could well be blatantly wrong.

      I found it rather funny that the above tutorial link goes to "regular-expressions.info", a domain which this regular expression would reject as invalid.

      Might consider changing it to read:

      (!preg_match("/^.+\@(\[?) [a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/", $email))
        Write a Reply...