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