正则替换可以使用函数

例如:替换字符串中所有#1.2.3.4#格式中的数字为0

import re

def replace(x):
    def _replace(matched):
        m = matched.group()
        change = re.sub("\d", '0', m)
        return change

    pattern = "#((\d*\.)+\d+)#"
    r = re.sub(pattern, _replace, x)
    return r

x = "ab.c.d.#1.0.3.4# 2.2.2asdf"
r = replace(x)

print x
print r

输出:

ab.c.d.#1.0.3.4# 2.2.2asdf
ab.c.d.#0.0.0.0# 2.2.2asdf

相关文章:

  • 2021-05-23
  • 2022-02-08
  • 2022-03-06
  • 2021-10-16
  • 2022-03-03
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-04
  • 2022-12-23
  • 2021-05-02
  • 2021-12-24
  • 2022-12-23
相关资源
相似解决方案