phpregexpreg-matcheregi

Warning: preg_match() [function.preg-match]: Unknown modifier '_'


Following is the regex I am trying to use

$eregicheck = "^([-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~])+@([-!#\$%&'*+/0-9=?A-Z^_`a-z{|}~]+\\.)+[a-zA-Z]{2,4}\$";

I changed the following lines

return eregi($eregicheck, $emailtocheck);

to

return preg_match($eregicheck, $emailtocheck);

But I dont know why I am getting the error

Warning: preg_match() [function.preg-match]: Unknown modifier '_'

Solution

  • You're getting this error because php requires delimiters before and after the regex pattern, which in your case it assumes to be ^ and after the delimiters follow the modifiers, in your case _. Since there's no such modifier hence the error. Change the code to:

    $eregicheck = "/^([-!#\$%&'*+.\/0-9=?A-Z^_`a-z{|}~])+@([-!#\$%&'*+\/0-9=?A-Z^_`a-z{|}~]+\\.)+[a-zA-Z]{2,4}\$/";
    

    P.S.: That seems like quite a complex regex, are you sure it can't be simplified? :P