Preg_Match() Unknown Modifier '[' Help

preg_match(); - Unknown modifier '+'

You need to use delimiters with regexes in PHP. You can use the often used /, but PHP lets you use any matching characters, so @ and # are popular.

Further Reading.

If you are interpolating variables inside your regex, be sure to pass the delimiter you chose as the second argument to preg_quote().

preg_match() Unknown modifier '[' help

Try the following:

<?php
$str = "http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I";
$pattern = '#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+#';
preg_match($pattern, $str, $matches);
print_r($matches);
?>

Note, I'm using # as a delimiter here simply because the regular expression above contains forward slashes and escaping them makes the expression more difficult to read. This cleans it up by just a few pixels.

preg_match error Unknown modifier '('

Escape / in the regular expression.

"/^([1-9]|0[1-9]|[1,2][0-9]|3[0,1])\/([1-9]|1[0,1,2])\/\d{4} ([0-1][0-9]|[2][0-3])(:([0-5][0-9])){1,2}(:([0-5][0-9])){1,2}$/"
# ^ ^

Otherwise, / is recognised as end of the regular expression.

Or use different delimiters:

"#^([1-9]|0[1-9]|[1,2][0-9]|3[0,1])/([1-9]|1[0,1,2])/\d{4} ([0-1][0-9]|[2][0-3])(:([0-5][0-9])){1,2}(:([0-5][0-9])){1,2}$#"

When does preg_match(): Unknown modifier error occur?

If the first name or last name contains a /, your regex will look something like:

/john/doe$/

To preg_match, this looks like the regex is /john/, with the trailing doe$/ being the modifiers. Those are of course invalid modifiers. You need to escape the regex delimiters (/) inside the regex itself using preg_quote.

Warning: preg_match(): Unknown modifier '.' after replacing ereg

  1. "." needs a "\" to avoid it be taken as any character.
  2. the pattern needs delimiters for preg_match.

    $pattern = "|([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)|";

PHP | preg_match(): Unknown modifier '(' in C:\xampp\htdocs\Folder\index.php on line 38

You forgot one "\" before "/)" on your pattern :

$ptn = "/^http:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/";

So php is thinking that "/(" is a modifier : http://php.net/manual/sr/reference.pcre.pattern.modifiers.php

PHP- Warning: preg_match(): Unknown modifier '('

Apply \ (backslash) before second /(forwardslash) to escape it

preg_match( '/Mozilla\/([0-9].[0-9]{1,2})/',$HTTP_USER_AGENT,$log_version);

preg_match('/Netscape([0-9])\/([0-9].[0-9]{1,2})/',$HTTP_USER_AGENT,$log_version1);

Note:- Any / in between starting and ending /(delementer) in preg_match() need to be escaped to make it run fine.



Related Topics



Leave a reply



Submit