Search code examples
regexfilter-var

Regular expressions mystery: "No ending delimiter '^' found"


Can someone explain to me? This is my code :

 $regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+'
            . '(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'; 
    if (preg_match($regex, $email)) {
        echo $email . " is a valid email. We can accept it.";
    } else { 
        echo $email . " is an invalid email. Please try again.";
    }

If I use for an example this regular expression:

^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)↪*(\.[a-z]{2,3})$

or another different regular expression I get:

preg_match() [function.preg-match]: No ending delimiter '^' found

And if I use filter_var('some_regular_expression', FILTER_EMAIL_VALIDATOR) an email like asd!qwe@gmail.com is a valid one ???


Solution

  • When you use the preg_XXX functions, the first character of the regular expression argument is a delimiter character -- it's / in your first example. It must appear at the end of the regular expression, it's used to separate the regular expression from modifier option characters, for instance in:

    /[a-z]/i
    

    the second / separates the regular expression from the i (case-insensitive) modifier.

    So you simply need to change your regular expression to:

    /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)↪*(\.[a-z]{2,3})$/
    

    I'm not sure what that character is in there for, though. It doesn't seem like it's appropriate for validating emails.

    And yes, asd!qwe@gmail.com is a valid email. Back in the day of UUCP mail transfer, addresses of the form host1!host2!host3!username@uu.net were common -- the ! characters delimited hostnames in the UUCP path. See Using a regular expression to validate an email address for recommendations on email validation.