The delimiter should not be an alphanumeric or backslash, but 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

+61
php regex preg-match
05 Oct '11 at 11:25
source share
6 answers

You need a separator for your template. It should be added at the beginning and end of the template as follows:

 $pattern = "/My name is '(.*)' and im fine/"; // With / as a delimeter 
+104
05 Oct '11 at 11:28
source share

The solution (other answers do not mention "mdash", at least during my initial writing) is that when PHP refers to delimiters, this does not apply to delimiters that you see in your code (which are quotation marks ) but the following characters are inside the string. (Actually, I have never seen this anywhere in any documentation: you should see it in the examples.) Therefore, instead of the regular expression syntax, as you are used to many other languages:

 /something/ 

PHP uses strings and then looks at the string for another :

 '/something/' 

The PHP delimiter refers to a pair of characters / instead of a pair of characters ' . Therefore, if you write 'something' , PHP will take s as the intended delimiter and complains that you are not allowed to use alphanumeric characters as the delimiter.

So, if you want to pass (for example) i to show that you want a match with the registration, you pass it inside the string, but outside the regular expression delimiters:

 '/something/i' 

If you want to use something other than / as a separator, you can, for example, if you match the URL and don't want it to remove all slashes:

 '~something~' 
+53
Apr 02 '14 at 15:23
source share

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

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

and below the same example, but with the modifier i so that it is not case sensitive.

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

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

Also remember that if you use the forward slash character (/) as a delimiter, you should then avoid further use of / in the regular expression if it is present.

+20
05 Oct '11 at 12:12
source share

The template must have delimiters. Separators can be slashes (/) 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@"; 
+5
May 13 '16 at 10:07
source share

Try with this

  $pattern = "/My name is '\(.*\)' and im fine/"; 
0
05 Oct '11 at 11:33
source share

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

 $string1 = "My name is 'Kate' and im fine"; $matches = pattern("My name is '(.*)' and im fine")->match($string1)->all(); // โ†‘ No delimiters needed 
0
May 23 '19 at 13:58
source share



All Articles