本文介绍如何在 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.
✅ 关键要点说明:
⚠️ 注意事项:
通过合理利用闭包与函数式替换接口,Go 完全可以优雅地完成“带序号的动态替换”任务——无需外部库,也无需手动遍历索引。