我试图创建一个Perl正则表达式,它匹配一个URL,该URL前面没有等号和一个单引号或双引号(可选),忽略空格. 下面的代码给出了一个错误:Warning: preg_replace(): Compilation failed: lookbehind assertion is not fixed length at offset 0
我知道我的URL正则表达式并不完美,但我更关注的是如何做负向后看,或者如何用其他方式表达.
例如,在下面的代码中,在匹配中,它应该输出http://www.url1.com/和http://www.url3.com/,而不是其他URL. 我该怎么办? 下面的代码给出了一个警告,并且不填充$matches变量.
PHP代码:
$html = "
http://www.url1.com/
= ' http://www.url2.com/
'http://www.url3.com/
<a href='http://www.url4.com/'>Testing1</a>
<img src='https://url5.com'>Testing2</a>";
$url_pregex = '((http(s)?://)[-a-zA-Z()0-9@:%_+.~#?&;//=]+)';
$pregex = '(?<!\\s*=\\s*[\'"]?\\s*)'.$url_pregex;
preg_match_all('`'.$pregex.'`i', $html, $matches);
echo "Matches<br><pre>";
var_export($matches);
echo "</pre>";
Perl Regex在PHP中使用`而不是/:
'`(?<!\\s*=\\s*[\'"]?\\s*)((http(s)?://)[-a-zA-Z()0-9@:%_+.~#?&;//=]+)`i'