Asked  7 Months ago    Answers:  5   Viewed   34 times

I have this regular expression:

([http://some.url.com/index.php?showtopic="]*)([0-9]+(?:.[0-9]*)?)

its for extracting links to topics from forum

Now when i use it in my script

$url = "([http://some.url.com/index.php?showtopic="]*)([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

 Answers

64

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:

([http://some.url.com/index.php?showtopic="]*)([0-9]+(?:.[0-9]*)?)
^                                             ^

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 [http://some.url.com/index.php?showtopic="] represents just one single character of the listed characters. So either h, t, p, :, /, etc. If you mean to express http://some.url.com/index.php?showtopic=" literally, use just http://some.url.com/index.php?showtopic=" (don’t forget to escape the meta characters).

Saturday, May 29, 2021
 
JohnnyW
answered 7 Months ago
15

This will work only for non-nested parentheses:

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

    $tags = preg_split($regex, $str, -1,
                         PREG_SPLIT_NO_EMPTY
                       | 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
 
KingCrunch
answered 9 Months ago
86

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: http://confluence.jetbrains.com/display/PhpStorm/PHPUnit+Installation+via+Composer+in+PhpStorm

Related usability ticket: http://youtrack.jetbrains.com/issue/WI-18388

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

Wednesday, March 31, 2021
 
ojrac
answered 9 Months ago
52

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
 
lewiguez
answered 7 Months ago
86

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
 
Fernando
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 :
 
Share