【Python】 搜索替换功能 sub()及subn()
sub()
a = "蓝天白云绿水青山" b = "蓝天大地绿水奔涌" c = re.compile("蓝天|青山") print(c.sub("aa_1", a, count=1)) print(c.sub("aa", a)) print(c.sub("bb", b))
执行结果:
subn()
a = "蓝天白云绿水青山" b = "蓝天大地绿水奔涌" c = re.compile("蓝天|青山") print(c.subn("aa_1", a, count=1)) print(c.subn("aa", a)) print(c.subn("bb", b))
执行结果:
-------------------------------------------------------------------------------------
如果万事开头难 那请结局一定圆满 @ Phoenixy
-------------------------------------------------------------------------------------