Quantcast
Channel: CodeProject Latest postings for Regular Expressions
Viewing all articles
Browse latest Browse all 224

PHP perl regular expression - URL not preceded by equal sign and possible single or double quote

$
0
0
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:
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'

Viewing all articles
Browse latest Browse all 224

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>