silent_dev
silent_dev

Reputation: 1616

How to remove words containing only numbers in python?

I have some text in Python which is composed of numbers and alphabets. Something like this:

s = "12 word word2"

From the string s, I want to remove all the words containing only numbers

So I want the result to be

s = "word word2"

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

re.sub('[\ 0-9\ ]+', ' ', line)

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

Thanks!

Upvotes: 5

Views: 6970

Answers (3)

Stam Kaly
Stam Kaly

Reputation: 668

Without using any external library you could do:

stringToFormat = "12 word word2"
words = ""
for word in stringToFormat.split(" "):
    try:
        int(word)
    except ValueError:
        words += "{} ".format(word)
print(words)

Upvotes: 1

Jon Clements
Jon Clements

Reputation: 142226

Using a regex is probably a bit overkill here depending whether you need to preserve whitespace:

s = "12 word word2"
s2 = ' '.join(word for word in s.split() if not word.isdigit())
# 'word word2'

Upvotes: 7

anubhava
anubhava

Reputation: 786091

You can use this regex:

>>> s = "12 word word2"
>>> print re.sub(r'\b[0-9]+\b\s*', '', s)
word word2

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

Upvotes: 10

Related Questions