Asked  7 Months ago    Answers:  5   Viewed   34 times

I have this regular expression:


its for extracting links to topics from forum

Now when i use it in my script

$url = "(["]*)([0-9]+(?:.[0-9]*)?)";

preg_match_all spits: "Unknown modifier '('"

This is also the call to preg_match

preg_match_all($url, $str, $matches,PREG_OFFSET_CAPTURE,3);

Can anyone help me with this obviously stupid problem



PCRE requires delimiters that separate the actual regular expression from optional modifiers. With PHP you can use any non-alphanumeric, non-backslash, non-whitespace character and even delimiters that come in pairs (brackets).

In your case the leading ( is used as delimiter and the first corresponding closing ) marks the end of the regular expression; the rest is treated as modifiers:

^                                             ^

But the first character after the ending delimiter (() is not a valid modifier. That why the error message says Unknown modifier '('.

In most cases / is used as delimiter like in Perl. But that would require to escape each occurrence of / in the regular expression. So it’s a good choice to choose a delimiter that’s not in the regular expression. In your case you could use # like BoltClock suggested.

Oh, and by the way: A character class like ["] represents just one single character of the listed characters. So either h, t, p, :, /, etc. If you mean to express" literally, use just" (don’t forget to escape the meta characters).

Saturday, May 29, 2021
answered 7 Months ago

This will work only for non-nested parentheses:

    $regex = <<<HERE
    /  "  ( (?:[^"\\]++|\\.)*+ ) "
     | '  ( (?:[^'\\]++|\\.)*+ ) '
     | ( ( [^)]*                  ) )
     | [s,]+

    $tags = preg_split($regex, $str, -1,
                       | PREG_SPLIT_DELIM_CAPTURE);

The ++ and *+ will consume as much as they can and give nothing back for backtracking. This technique is described in perlre(1) as the most efficient way to do this kind of matching.

Wednesday, March 31, 2021
answered 9 Months ago

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario:

Related usability ticket:

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
answered 9 Months ago

The standard disclaimer applies: Parsing HTML with regular expressions is not ideal. Success depends on the well-formedness of the input on a character-by-character level. If you cannot guarantee this, the regex will fail to do the Right Thing at some point.

Having said that:

<ab[^>]*>(.*?)</a>   // match group one will contain the link text
Saturday, May 29, 2021
answered 7 Months ago

Look which resource file is being loaded by examining $_SERVER['PHPRC']. This value points to the directory where PHP looks for php.ini. I doubt that it's /etc.

Then install the right package, which is php-mcrypt.

Thursday, August 19, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :