Python regex to remove all words which contains number

后端 未结 2 1015
夕颜
夕颜 2020-12-16 00:01

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         


        
2条回答
  •  佛祖请我去吃肉
    2020-12-16 00:34

    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'
    

提交回复
热议问题