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>You want an inverted character class:</p>

<pre><code>re.sub(r"[^\d.]", "", x)
</code></pre>

<p>Note that <code>[^0-9.]</code> and <code>[^\d.]</code> are not the same, because <code>\d</code> matches many more characters than just <code>0123456789</code>:</p>

<pre><code>&gt;&gt;&gt; print(textwrap.fill(
...    "".join(x for x in (chr(y) for y in range(0x110000))
...            if re.match(r"\d", x)),
...    break_long_words=True, width=10))
0123456789
Ù Ù¡Ù¢Ù£Ù¤Ù¥Ù¦Ù§Ù¨Ù©
Û°Û±Û²Û³Û´ÛµÛ¶Û·Û¸Û¹
ßßßßßßßßßß
à¥¦à¥§à¥¨à¥©à¥ªà¥«à¥¬à¥­à¥®à¥¯
à§¦à§§à§¨à§©à§ªà§«à§¬à§­à§®à§¯
à©¦à©§à©¨à©©à©ªà©«à©¬à©­à©®à©¯
à«¦à«§à«¨à«©à«ªà««à«¬à«­à«®à«¯
à­¦à­§à­¨à­©à­ªà­«à­¬à­­à­®à­¯
à¯¦à¯§à¯¨à¯©à¯ªà¯«à¯¬à¯­à¯®à¯¯
à±¦à±§à±¨à±©à±ªà±«à±¬à±­à±®à±¯
à³¦à³§à³¨à³©à³ªà³«à³¬à³­à³®à³¯
àµ¦àµ§àµ¨àµ©àµªàµ«àµ¬àµ­àµ®àµ¯
à·¦à·§à·¨à·©à·ªà·«à·¬à·­à·®à·¯
à¹à¹à¹à¹à¹à¹à¹à¹à¹à¹
à»à»à»à»à»à»à»à»à»à»
à¼ à¼¡à¼¢à¼£à¼¤à¼¥à¼¦à¼§à¼¨à¼©
áááááááááá
áááááááááá
á á¡á¢á£á¤á¥á¦á§á¨á©
á á á á á á á á á á 
á¥á¥á¥á¥á¥á¥á¥á¥á¥á¥
á§á§á§á§á§á§á§á§á§á§
áªáªáªáªáªáªáªáªáªáª
áªáªáªáªáªáªáªáªáªáª
á­á­á­á­á­á­á­á­á­á­
á®°á®±á®²á®³á®´á®µá®¶á®·á®¸á®¹
á±á±á±á±á±á±á±á±á±á±
á±á±á±á±á±á±á±á±á±á±
ê ê¡ê¢ê£ê¤ê¥ê¦ê§ê¨ê©
ê£ê£ê£ê£ê£ê£ê£ê£ê£ê£
ê¤ê¤ê¤ê¤ê¤ê¤ê¤ê¤ê¤ê¤
ê§ê§ê§ê§ê§ê§ê§ê§ê§ê§
ê§°ê§±ê§²ê§³ê§´ê§µê§¶ê§·ê§¸ê§¹
ê©ê©ê©ê©ê©ê©ê©ê©ê©ê©
ê¯°ê¯±ê¯²ê¯³ê¯´ê¯µê¯¶ê¯·ê¯¸ê¯¹
ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼
í í² í í²¡í í²¢í í²£í í²¤í í²¥í í²¦í í²§í í²¨í í²©
í í±¦í í±§í í±¨í í±©í í±ªí í±«í í±¬í í±­í í±®í í±¯
í í³°í í³±í í³²í í³³í í³´í í³µí í³¶í í³·í í³¸í í³¹
í í´¶í í´·í í´¸í í´¹í í´ºí í´»í í´¼í í´½í í´¾í í´¿
í í·í í·í í·í í·í í·í í·í í·í í·í í·í í·
í í»°í í»±í í»²í í»³í í»´í í»µí í»¶í í»·í í»¸í í»¹
í í³í í³í í³í í³í í³í í³í í³í í³í í³í í³
í í¹í í¹í í¹í í¹í í¹í í¹í í¹í í¹í í¹í í¹
í í»í í»í í»í í»í í»í í»í í»í í»í í»í í»
í í¼°í í¼±í í¼²í í¼³í í¼´í í¼µí í¼¶í í¼·í í¼¸í í¼¹
í í³ í í³¡í í³¢í í³£í í³¤í í³¥í í³¦í í³§í í³¨í í³©
í í¹ í í¹¡í í¹¢í í¹£í í¹¤í í¹¥í í¹¦í í¹§í í¹¨í í¹©
í í½í í½í í½í í½í í½í í½í í½í í½í í½í í½
í µí¿í µí¿í µí¿í µí¿í µí¿í µí¿í µí¿í µí¿í µí¿í µí¿
í µí¿í µí¿í µí¿í µí¿í µí¿í µí¿í µí¿í µí¿í µí¿ í µí¿¡
í µí¿¢í µí¿£í µí¿¤í µí¿¥í µí¿¦í µí¿§í µí¿¨í µí¿©í µí¿ªí µí¿«
í µí¿¬í µí¿­í µí¿®í µí¿¯í µí¿°í µí¿±í µí¿²í µí¿³í µí¿´í µí¿µ
í µí¿¶í µí¿·í µí¿¸í µí¿¹í µí¿ºí µí¿»í µí¿¼í µí¿½í µí¿¾í µí¿¿
</code></pre>

<p>I bet you didn't know there were so many variations of the <a href="https://en.wikipedia.org/wiki/Hindu%E2%80%93Arabic_numeral_system" rel="nofollow">Hindu-Arabic numeral system</a>.</p>

<p>It's also worth mentioning that even in the latest 3.x, Python's regular expressions do <strong>not</strong> support <a href="http://www.regular-expressions.info/posixbrackets.html" rel="nofollow">POSIX ERE named character classes</a> (scroll down to "character classes" -- sadly, there is no anchor).  <code>[^[:digit:].]</code> won't do what you want.</p>

python regex python-2.7