使用\d
的数字:
>>> import re
>>> g = "C0N4rtist"
>>> re.sub(r'\d+', '', g)
'CNrtist'
请注意,您不需要正则表达式,str.translate
与正则表达式版本相比非常快
>>> from string import digits
>>> g.translate(None, digits)
'CNrtist'
>>> g = "C0N4rtist"*100
>>> %timeit g.translate(None, digits) #winner
100000 loops, best of 3: 9.98 us per loop
>>> %timeit ''.join(i for i in g if not i.isdigit())
1000 loops, best of 3: 507 us per loop
>>> %timeit re.sub(r'\d+', '', g)
1000 loops, best of 3: 253 us per loop
>>> %timeit ''.join([i for i in g if not i.isdigit()])
1000 loops, best of 3: 352 us per loop
>>> %timeit ''.join([i for i in g if i not in digits])
1000 loops, best of 3: 277 us per loop