negative lookbehind and greedy quantifiers in php

pkg

New Member
I'm using a regex to find any URLs and link them accordingly. However, I do not want to linkify any URLs that are already linked so I'm using lookbehind to see if the URL has an href before it.This fails though because variable length quantifiers aren't allowed in lookahead and lookbehind for PHP.Here's the regex for the match:\[code\]/\b(?<!href\s*=\s*[\'\"])((?:http:\/\/|www\.)\S*?)(?=\s|$)/i\[/code\]What's the best way around this problem?EDIT:I have yet to test it, but I think the trick to doing it in a single regex is using conditional expressions within the regex, which is supported by PCRE. It would look something like this:\[code\]/(href\s*=\s*[\'\"])?(?(1)^|)((?:http:\/\/|www\.)\w[\w\d\.\/]*)(?=\s|$)/i\[/code\]The key point is that if the href is captured, the match is immediately thrown out due to the conditional \[code\](?(1)^|)\[/code\], which is guaranteed to not match.There's probably something wrong with it. I'll test it out tomorrow.
 
Back
Top