sub()方法:替换符合规律的内容,返回替换的值
# -?- coding: utf-8 -?-import resecret_code = 'wwwfhskdjwww'secret_code_new = re.sub('www(.*?)www', 'www.aobosir.www', secret_code )print(secret_code_new)运行
www.aobosir.www上面的代码也也可以写成这个样子,替换格式化字符串。使用占位符:
# -?- coding: utf-8 -?-import resecret_code = 'wwwfhskdjwww'secret_code_new = re.sub('www(.*?)www', 'www%swww'%'.aobosir.', secret_code )print(secret_code_new)运行
www.aobosir.www参考网站:
http://www.jikexueyuan.com/course/777_2.html?ss=2