简体   繁体   中英

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 ie 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!

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'

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM