title: Regex - negative lookahead with lazy limit?
body: <p>I'm not a regex expert, but after a couple hours I've built this regex:</p>
  
  <pre><code>#\[url=(?!.*?&lt;div onclick="unveil_spoiler.*?\[/url\])([^_\W]+?://.*?)\](.+?)\[/url\]#i
  </code></pre>
  
  <p>Which is a case-insensitive:</p>
  
  <pre><code>\[url=(?!.*?&lt;div onclick="unveil_spoiler.*?\[/url\])([^_\W]+?://.*?)\](.+?)\[/url\]
  </code></pre>
  
  <p>To match <code>[url=xxxx://yyyy]zzzz[/url]</code> patterns, except if it contains a <code>&lt;div onclick="unveil_spoiler</code> string between <code>[url=</code> and and <code>[/url]</code>.</p>
  
  <p>Now I'm trying to add a similar check, to don't return a match if it contains a <code>\[url.*?\]</code> between the <code>\[url=</code> and <code>\[/url\]</code>. I've tried many ways but I can seem to find a 100% working one.</p>
  
  <p>First I tried adding another negative lookahead very similar to the one already present in my regex, which works partially, but then it seems like the lookahead goes through until the end of the line - until the last <code>\[/url\]</code> - for each match, I wanted the lookahead to stop at the first <code>\[/url\]</code> as the capturing group does.</p>
  
  <p>Here's a string for debugging:</p>
  
  <pre><code>[url=http://www.match.com]Match[/url][url=http://www.nomatch.com&lt;div onclick="unveil_spoiler"]No match[/url][url=http://www.match.com]Match[/url][url=http://www.nomatch.com]&lt;div onclick="unveil_spoiler" No match[/url]
  [url=http://www.nomatch.com]No &lt;div onclick="unveil_spoiler"match[/url][url=http://www.match.com]Match[/url][url=http://www.nomatch.com]No &lt;div onclick="unveil_spoiler" match[/url][url=http://www.match.com]Match[/url]
  
  [url=http://www.match.com]Match[/url][url=http://www.match.com][b]Match[/b][/url][url=http://www.match.com]Match[/url][url=http://www.match.com]Match[/url]
  
  [url=http://www.thisshouldntmatch.com[url=http://www.match.com]Match[/url]This shouldn't match[/url]
  
  [url=http://www.thisshouldntmatch.com[url=http://www.thisshouldntmatch.com[url=http://www.match.com]Match[/url]]This shouldn't match[/url]This shouldn't match[/url]
  
  [url=http://www.thisshouldntmatch.com[url=http://www.match.com]Match[/url]This shouldn't match[/url][url=http://www.match.com]Match[/url]
  
  [url=http://www.thisshouldntmatch.com]This shouldn't match[url=http://www.match.com]Match[/url][url=http://www.match.com]Match[/url][/url]
  
  [url=http://www.match.com]Match[/url][url=http://www.match.com]Match[/url][url=http://www.match.com]Match[/url][url=http://www.match.com]Match[/url]
  </code></pre>
  
  <p>With the regex posted in the beginning of the post, it'll match the 2 matches in the first line perfectly. Now I wanted it to don't return a match when there's a <code>\[url.*?\]</code> inside of the match, I've tried this regex:</p>
  
  <pre><code>\[url=(?!.*?\[url.*?\].*?\[/url.*?\])(?!.*?&lt;div onclick="unveil_spoiler.*?\[/url\])([^_\W]+?://.*?)\](.+?)\[/url\]
  </code></pre>
  
  <p>And this:</p>
  
  <pre><code>\[url=(?!.*?(?:&lt;div onclick="unveil_spoiler|\[url.*?\]).*?\[/url\])([^_\W]+?://.*?)\](.+?)\[/url\]
  </code></pre>
  
  <p>Which won't return matches when there's a <code>\[url.*?\]</code> inside of the match, but then it also stops matching the first match of the first line (in the example string) which it should (and as the first regex does). That is, it'll only match the last valid match of each line then.</p>
  
  <p>I think it's a problem with the lookahead which doesn't stop at the first <code>\[/url\]</code>, is there any way to make it lazy/fix it then?</p>
  
  <p>Any help is appreciated.</p>