I'm trying to create a perl regular expression that matches a URL that is not preceded by an equal sign and one single or double quote (optional) ignoring whitespace. The code below gives an error:
I know my URL regular expression isn't perfect, but I'm more focused on how to do the negative lookbehind or how to express this in some other way.
For example, in the code below, I would like for the expression to match http://www.url1.com/ and http://www.url3.com/, but not the other URLs. How can I do this? The code below gives a warning and does not populate the $matches variable.
The Code:
Perl Regex in PHP, using ` instead of /:
Warning: preg_replace(): Compilation failed: lookbehind assertion is not fixed length at offset 0
I know my URL regular expression isn't perfect, but I'm more focused on how to do the negative lookbehind or how to express this in some other way.
For example, in the code below, I would like for the expression to match http://www.url1.com/ and http://www.url3.com/, but not the other URLs. How can I do this? The code below gives a warning and does not populate the $matches variable.
The Code:
$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; $output = preg_match('`'.$pregex.'`i', $html, $matches); preg_match('`'.$pregex.'`i', $html, $matches); var_export($matches);
Perl Regex in PHP, using ` instead of /:
PHP
'`(?<!\\s*=\\s*[\'"]?\\s*)((http(s)?://)[-a-zA-Z()0-9@:%_+.~#?&//=]+)`i'