removing a string using Regex and lookahead
<p>I have one string which is like this:</p>

<pre><code>&lt;/p&gt;\r\n&lt;p&gt;&lt;img src="https://domain.com/article/id/image.png" &gt;
</code></pre>

<p>Now I want to remove "<a href="https://domain.com" rel="nofollow">https://domain.com</a>" from src if it is there without token, for example</p>

<pre><code>&lt;/p&gt;\r\n&lt;p&gt;&lt;img src="https://domain.com/article/id/image.png" &gt;
</code></pre>

<p>here I would like to remove <a href="https://domain.com" rel="nofollow">https://domain.com</a></p>

<p>Another example</p>

<pre><code>&lt;/p&gt;\r\n&lt;p&gt;&lt;img src="https://domain.com/article/token/123/id/image.png" &gt;
</code></pre>

<p>In this case I don't want to replace <a href="https://domain.com" rel="nofollow">https://domain.com</a> since it contains <strong>token</strong>
So I have written the following code</p>

<pre><code>re.sub('(?!.*/token/).*(img src="https://domain\.com)','img src="',a)
</code></pre>

<p>I successfully removes the domain but it also removes <strong>&lt;</strong> of the img</p>

<p>The output I am getting is </p>

<pre><code>&lt;/p&gt;\r\nimg src="/article/id/image.png" &gt;
</code></pre>

<p>while I am expecting </p>

<pre><code>&lt;/p&gt;\r\n&lt;img src="/article/id/image.png" &gt;
</code></pre>

<p>I know I can get the desired output by adding &lt; before img in substitution , or by any other method such as string.replace or any other but I am trying to understand this behaviour.</p>