【问题标题】:Alternative to negative look ahead消极展望的替代方案
【发布时间】:2021-10-05 14:49:03
【问题描述】:

我正在编写一个 Terraform 模块,我需要在其中捕获和替换字符串中的标记。令牌由{{...}} 括起来,但我不想捕获与{{global-...}} 匹配的令牌。

例如:

locals {
    input = "Hello {{name}} this is {{global-env}}. Welcome to {{home}}."
    output = replace(local.input, "/{{(.+?)}}/", "{{$1-en_US}}")
}

output "output" { value = local.output }
# Actual:   Hello {{name-en_US}} this is {{global-env-en_US}}. Welcome to {{home-en_US}}
# Expected: Hello {{name-en_US}} this is {{global-env}}. Welcome to {{home-en_US}}

在 C# 正则表达式中,我可以使用否定的前瞻性:

{{(?!global-)(.+?)}}

但是,由于 Terraform 及其底层的 Go 语言运行时不支持环视,还有什么替代方案?位于 Terraform 模块中也不允许我遍历每个匹配项并测试捕获组。

【问题讨论】:

    标签: regex go terraform


    【解决方案1】:

    您可以通过使用第二个替换来解决此问题,以便在第一次替换后从以 global- 开头并以 -en_US 结尾的占位符中删除 -en_US

    output = replace(replace(local.input, "/{{(.+?)}}/", "{{$1-en_US}}"), "({{global-.+?)-en_US}}", "$1}}")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-20
      • 2010-09-09
      • 2023-02-04
      • 2018-01-31
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      • 2017-05-12
      相关资源
      最近更新 更多