How to remove words containing only numbers in python?
<p>I have some text in Python which is composed of numbers and alphabets. Something like this:</p>

<pre><code>s = "12 word word2"
</code></pre>

<p>From the string s, I want to remove all the words containing <strong>only numbers</strong></p>

<p>So I want the result to be </p>

<pre><code>s = "word word2"
</code></pre>

<p>This is a regex I have but it works on alphabets i.e. it replaces each alphabet by a space.</p>

<pre><code>re.sub('[\ 0-9\ ]+', ' ', line)
</code></pre>

<p>Can someone help in telling me what is wrong? Also, is there a more time-efficient way to do this than regex?</p>

<p>Thanks!</p>

<p>You can use this regex:</p>

<pre><code>&gt;&gt;&gt; s = "12 word word2"
&gt;&gt;&gt; print re.sub(r'\b[0-9]+\b\s*', '', s)
word word2
</code></pre>

<p><code>\b</code> is used for word boundary and <code>\s*</code> will remove 0 or more spaces after your number word.</p>

python regex string