Python正则表达式删除包含数字的所有单词

Que*_*roi 10 python regex

我正在尝试制作一个Python正则表达式,它允许我删除包含数字的字符串的所有世界.

例如:

in = "ABCD abcd AB55 55CD A55D 5555"
out = "ABCD abcd"
Run Code Online (Sandbox Code Playgroud)

删除号码的正则表达式是微不足道的:

print(re.sub(r'[1-9]','','Paris a55a b55 55c 555 aaa'))
Run Code Online (Sandbox Code Playgroud)

但我不知道如何删除整个单词,而不仅仅是数字.

请问你能帮帮我吗?

ars*_*jii 19

你需要正则表达式吗?你可以做点什么

>>> words = "ABCD abcd AB55 55CD A55D 5555"
>>> ' '.join(s for s in words.split() if not any(c.isdigit() for c in s))
'ABCD abcd'
Run Code Online (Sandbox Code Playgroud)

如果你真的想使用正则表达式,你可以尝试\w*\d\w*:

>>> re.sub(r'\w*\d\w*', '', words).strip()
'ABCD abcd'
Run Code Online (Sandbox Code Playgroud)


iCo*_*dez 8

这是我的方法:

>>> import re
>>> s = "ABCD abcd AB55 55CD A55D 5555"
>>> re.sub("\S*\d\S*", "", s).strip()
'ABCD abcd'
>>>
Run Code Online (Sandbox Code Playgroud)