这就是我到目前为止:
match = re.sub(r'[0-9]',"","th1s n33ds to be r3m0v3d and this 2 doesnt")
现在这将删除整个句子中的所有数字,我尝试了一切。 有没有人对此有所了解?
非常感谢
答案 0 :(得分:2)
您可以使用\B
:
>>> re.sub(r'\B[0-9]+\B',"","th1s n33ds to be r3m0v3d and this 2 doesnt")
ths nds to be rmvd and this 2 doesnt
从正则表达式转换为英语:删除位于单词内的所有数字序列。
\ B - 匹配空字符串,但仅当它不在时 一个词的开头或结尾。
编辑:如果数字可以开始或结束该单词,则此正则表达式将执行:
>>> re.sub(r'([0-9]+(?=[a-z])|(?<=[a-z])[0-9]+)',"","1th1s n33ds to be r3m0v3d and this 2 doesnt3")
ths nds to be rmvd and this 2 doesnt
从正则表达式翻译成英文:删除所有数字后跟或前面的字母。 第二个正则表达式非常难看,我确信有更好的方法。
答案 1 :(得分:0)
这有效 -
re.sub(r'(?:[a-zA-Z]*[0-9]+[a-zA-Z]+)|(?:[a-zA-Z]+[0-9]+[a-zA-Z]*)',"","th1s n33ds to be r3m0v3d and this 2 doesnt this2")
# output
' to be and this 2 doesnt '