如何在 Go 中使用正则表达式实现带计数器的字符串替换

本文介绍如何在 go 中借助 `regexp.replaceallstringfunc` 和闭包变量实现按匹配顺序递增编号的字符串替换,适用于日志标记、代码注释编号、文本批量标注等场景。

在 Go 的 regexp 包中,标准替换方法(如 ReplaceAllString 或 ReplaceAll)不支持动态状态(如计数器),因为它们要求替换字符串是纯静态的。但 Go 提供了更灵活的函数式接口——ReplaceAllStringFunc,它接受一个匹配字符串到替换字符串的映射函数,允许我们在回调中维护闭包状态,从而实现“每匹配一次,计数加一”的效果。

以下是一个完整、可运行的示例:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    input := `Let freedom ring from the mighty mountains of New York. Let freedom ring from the heightening Alleghenies of Pennsylvania. Let freedom ring from the snow-capped Rockies of Colorado. Let freedom ring from the curvaceous slopes of California.`

    r := regexp.MustCompile(`Let freedom`)
    i := 0 // 闭包内共享的计数器

    result := r.ReplaceAllStringFunc(input, func(m string) string {
        i++
        if i == 1 {
            return fmt.Sprintf("[%d] %s", i, m)
        }
        return fmt.Sprintf("[%d] %s%d", i, m, i)
    })

    fmt.Println(result)
}

输出结果:

[1] Let freedom ring from the mighty mountains of New York. [2] Let freedom2 ring from the heightening Alleghenies of Pennsylvania. [3] Let freedom3 ring from the snow-capped Rockies of Colorado. [4] Let freedom4 ring from the curvaceous slopes of California.

关键要点说明:

  • ReplaceAllStringFunc 对每个匹配项调用一次传入的函数,且保证按文本从左到右的顺序执行,因此计数器 i 的递增与匹配位置严格对应;
  • 计数器 i 必须定义在 ReplaceAllStringFunc 调用外部(即闭包作用域内),否则每次调用函数都会重置;
  • 若需更复杂的逻辑(如跳过某些匹配、条件重置计数器),可在回调函数中自由添加判断;
  • 注意:该方法仅适用于全字符串匹配替换(即替换整个匹配串)。若需保留原始匹配内容并插入编号(例如在原位置前/后插入),应改用 ReplaceAllStringSubmatchFunc 或 ReplaceAllFunc(Go 1.22+)。

⚠️ 注意事项:

  • 此方案不是并发安全的。若在 goroutine 中并发调用该替换逻辑,需额外加锁或改用 sync/atomic;
  • 正则表达式本身未启用捕获组,因此无需担心 m 内容被截断——ReplaceAllStringFunc 传入的 m 就是完整匹配的字符串;
  • 如需全局唯一计数(跨多次调用),建议将计数器封装为结构体字段或使用原子操作管理。

通过合理利用闭包与函数式替换接口,Go 完全可以优雅地完成“带序号的动态替换”任务——无需外部库,也无需手动遍历索引。