Python regex to remove all words which contains number

后端 未结 2 1011
夕颜
夕颜 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'
    
    0 讨论(0)
  • 2020-12-16 00:49

    Here's my approach:

    >>> import re
    >>> s = "ABCD abcd AB55 55CD A55D 5555"
    >>> re.sub("\S*\d\S*", "", s).strip()
    'ABCD abcd'
    >>>
    
    0 讨论(0)
提交回复
热议问题