在re.sub()

时间:2017-10-12 06:13:48

标签: regex python-3.x

我正在处理一个字符串,我想删除一些特殊字符。此外,我想从中删除单个字符和数字值,但不删除字母数字值。目前,我必须多次使用re.sub()来执行该操作。我可以在一个re.sub()中使用多个条件吗?

sample = "[ This - is a 26 sample s string with ? special . characters' s26]"
sample = re.sub(r'[^a-zA-Z0-9._ "]'," ",sample)
sample = re.sub(r'\b[0-9 .]+\b',"",sample)
sample = re.sub(r'\b[a b c d e f g ..... z]+\b',"",sample)

预期产出:

sample = "This is sample string with special characters s26"

0 个答案:

没有答案