How to use regex to disallow non-digts but allow a dot in Python?
<p>I am new to regex and trying to remove all <strong>non-digts</strong> but keep the <strong>dot</strong> (<strong>.</strong>) of a string:</p>

<pre><code>x = ['ABCD, EFGH ', ' 20.9&amp;dog; ', ' IJKLM /&gt;']
</code></pre>

<p>So far I have tried the following:</p>

<pre><code>&gt;&gt;&gt; x = re.sub("\D", "", x)
209
</code></pre>

<p>However I am trying to get the following outcome:</p>

<pre><code>20.9
</code></pre>

<p>Thanks.</p>

<p>All the answers have this issue of skipping DOT without making sure that DOT is actually part of a decimal number. Hence a string like <code>Mr.Bean</code> will remain <code>Mr.Bean</code> since DOT is is part of the negative character class (exclusion list).</p>

<p>To fix tihs issue you can use this negative lookahead regex:</p>

<pre><code>&gt;&gt;&gt; re.sub(r"(?!\.\d)\D", "", ' 20.9&amp;dog; ')
'20.9'
</code></pre>

<p><code>(?!\.\d)</code> asserts that when we match <code>\D</code> we don't have a DOT and a digit ahead.</p>

python regex python-2.7