Python remove anything that is not a letter or number

后端 未结 7 1295
甜味超标
甜味超标 2020-12-24 01:40

I\'m having a little trouble with Python regular expressions.

What is a good way to remove all characters in a string that are not letters or numbers?

Thanks

7条回答
  •  别那么骄傲
    2020-12-24 02:03

    you can use predefined regex in python : \W corresponds to the set [^a-zA-Z0-9_]. Then,

    import re
    s = 'Hello dutrow 123'
    re.sub('\W', '', s)
    --> 'Hellodutrow123'
    

提交回复
热议问题