语法:
re.sub(pattern, repl, string, max=0)
例如:
import re str = "My name is David. Hi David." pattern = r"David" newstr = re.sub(pattern, "Amy", str) print(newstr)
结果:
>>> My name is Amy. Hi Amy. >>>
1