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

121 Views Asked by At

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 '_'
2

There are 2 best solutions below

5
On BEST ANSWER

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

1
On

Try:

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

You need to escape ^. It is a special character, which provides instructions to RE.