问题
how do I get the regex mentioned in this article working with preg_match in php?
<?php
preg_match("\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))/i", $text, $matches);
print_r($matches);
?>
Using the code above I get the following error:
Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash...
回答1:
Try this:
preg_match("#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#i", $text, $matches);
You were missing the regex delimiters (usually /
, but using #
here because it's more convenient for URLs)
来源:https://stackoverflow.com/questions/2025095/url-matching-using-grubers-regex-in-php