import sys,re t='a2b3c4zhhahhho12/13 222a5555(fuck)669aaaaaa9' rep=r'2|3|4|hh|[0-9]+/[0-9]+|9.*9|\(.*\)' t= re.sub(rep,'',t) print t
规则[0-9]+/[0-9]+用来删除12/13
规则9.*9用来删除9aaaaaa9
规则\(.*\)用来删除(fuck)
mathch=re.compile('abc(.*)def').search('ddabcXXXXXXXXXdef') print mathch.group(0) print mathch.group(1)
结果:
abcXXXXXXXXXdef XXXXXXXXX