Why is this regex expression not working?
<p>I want to separate out the links from the string which don't have ':' in between and do not end with '.jpg' or '.svg', and also start with '/wiki/'.</p>

<p>So these are wrong - </p>

<pre><code>"https://boomerrang.com"
"/wiki/sbsbs:kjanw"
"/wiki/aswaa:asawsa.jpg"
"/wiki/awssa.random.jpg"
"/wiki/boom.jpg"
</code></pre>

<p>How the final result should look like - </p>

<pre><code>"/wiki/justthis"
</code></pre>

<p><strong>What I tried -</strong> </p>

<pre><code>r'^/wiki/.*[^:](?!jpg|svg)$'
</code></pre>

<p>But its not evaluating properly, infact its giving all the result which I do not want... I'm kind of new to regex, so please tell me why this is not working, and how should I correct it.</p>

<p>Thanks</p>

<p>Why your pattern doesn't work:</p>

<p><code>.*[^:]</code> doesn't prevent a <code>:</code> to be present in the string since <code>.*</code> can match it.</p>

<p><code>(?!jpg|svg)$</code> doesn't make sense since it says that the end of the string isn't followed by "jpg" or "svg". Obviously the end of the string isn't followed by anything since it's the end of the string. Keep in mind that a lookaround (lookahead or lookbehind), anchors like <code>^</code>, <code>$</code> or a word-boundary <code>\b</code> are <em>zero-width assertions</em> and don't consume characters, so <code>(?!jpg|svg)</code> and <code>$</code> are tested from the same position in the string.</p>

<p>You can try that:</p>

<pre><code>r'^/wiki/[^:]*(?&lt;!\.jpg)(?&lt;!\.svg)$'
</code></pre>

<p>The two negative lookbehinds at the end ensure that the string doesn't end with <code>.svg</code> or <code>.jpg</code>.</p>

<p><code>[^:]*</code> avoids any <code>:</code> in the string.</p>

python regex