[php] Delimiter must not be alphanumeric or backslash and preg_match

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

This question is related to php regex preg-match

The answer is


You must specify a delimiter for your expression. A delimiter is a special character used at the start and end of your expression to denote which part is the expression. This allows you to use modifiers and the interpreter to know which is an expression and which are modifiers. As the error message states, the delimiter cannot be a backslash because the backslash is the escape character.

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

and below the same example but with the i modifier to match without being case sensitive.

$pattern = "/My name is '(.*)' and im fine/i";

As you can see, the i is outside of the slashes and therefore is interpreted as a modifier.

Also bear in mind that if you use a forward slash character (/) as a delimiter you must then escape further uses of / in the regular expression, if present.


The pattern must have delimiters. Delimiters can be a forward slash (/) or any non alphanumeric characters(#,$,*,...). Examples

$pattern = "/My name is '(.*)' and im fine/"; 
$pattern = "#My name is '(.*)' and im fine#";
$pattern = "@My name is '(.*)' and im fine@";  

The solution (which other answers don't mention—at least at the time of my originally writing this) is that when PHP refers to delimiters, it's not referring to the delimiters you see in your code (which are quote marks) but the next characters inside the string. (In fact I've never seen this stated anywhere in any documentation: you have to see it in examples.) So instead of having a regular expression syntax like what you may be accustomed to from many other languages:

/something/

PHP uses strings, and then looks inside the string for another delimiter:

'/something/'

The delimiter PHP is referring to is the pair of / characters, instead of the pair of ' characters. So if you write 'something', PHP will take s as the intended delimiter and complain that you're not allowed to use alphanumeric characters as your delimiter.

So if you want to pass (for instance) an i to show that you want a case-insensitve match, you pass it inside the string but outside of the regex delimiters:

'/something/i'

If you want to use something other than / as your delimiter, you can, such as if you're matching a URL and don't want to have to escape all the slashes:

'~something~'

Maybe not related to original code example, but I received the error "Delimiter must not be alphanumeric or backslash" and Googled here. Reason: I mixed order of parameters for preg_match. Pattern was the second parameter and string to match was first. Be careful :)


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 

You can also use T-Regx library which has automatic delimiters for you:

$matches = pattern("My name is '(.*)' and im fine")->match($string1)->all();

                 // ? No delimiters needed 

Please try with this

 $pattern = "/My name is '\(.*\)' and im fine/"; 

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to regex

Why my regexp for hyphenated words doesn't work? grep's at sign caught as whitespace Preg_match backtrack error regex match any single character (one character only) re.sub erroring with "Expected string or bytes-like object" Only numbers. Input number in React Visual Studio Code Search and Replace with Regular Expressions Strip / trim all strings of a dataframe return string with first match Regex How to capture multiple repeated groups?

Examples related to preg-match

Preg_match backtrack error Regular expression containing one word or another How to search in an array with preg_match? PHP preg_match - only allow alphanumeric strings and - _ characters Delimiter must not be alphanumeric or backslash and preg_match PHP is_numeric or preg_match 0-9 validation Regex: Specify "space or start of string" and "space or end of string" PHP regular expressions: No ending delimiter '^' found in PHP - regex to allow letters and numbers only