Delimiter must not be alphanumeric or backslash and preg_match

user547363 picture user547363 · Oct 5, 2011 · Viewed 153.9k times · Source

I have this code :

$string1 = "My name is 'Kate' and im fine"; 
$pattern = "My name is '(.*)' and im fine"; 
preg_match($pattern , $string1, $matches);
echo $matches[1];

and when im run it returns this error:

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash

Answer

Paul picture Paul · Oct 5, 2011

You need a delimiter for your pattern. It should be added at the start and end of the pattern like so:

$pattern = "/My name is '(.*)' and im fine/";  // With / as a delimeter