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>Using a regex is probably a bit overkill here depending whether you need to preserve whitespace:</p>

<pre><code>s = "12 word word2"
s2 = ' '.join(word for word in s.split() if not word.isdigit())
# 'word word2'
</code></pre>

python regex string