且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-17 23:01:35

您需要正则表达式吗?你可以做类似的事情

>>>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'

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

>>>re.sub(r'\w*\d\w*', '', words).strip()'ABCD abcd'

I am trying to make a Python regex which allows me to remove all worlds of a string containing a number.

For example:

in = "ABCD abcd AB55 55CD A55D 5555"
out = "ABCD abcd"

The regex for delete number is trivial:

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

But I don't know how to delete the entire word and not just the number.

Could you help me please?

Do you need a regex? You can do something like

>>> 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'

If you really want to use regex, you can try \w*\d\w*:

>>> re.sub(r'\w*\d\w*', '', words).strip()
'ABCD abcd'