Python正则表达式删除所有包含数字的单词 [英] Python regex to remove all words which contains number

查看:47
本文介绍了Python正则表达式删除所有包含数字的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

例如:

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

删除数字的正则表达式很简单:

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

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

你能帮我吗?

解决方案

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

<预><代码>>>>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'

这篇关于Python正则表达式删除所有包含数字的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆