douxidang9092 2019-08-12 18:34
浏览 90
已采纳

如何匹配彼此相邻的包围支架?

I'm am attempting to parse lines of Helm templates. I run into issues when I have value declarations side-by-side on the same line.

For example, the line image: "{{ $.Values.image.repository }}:{{ $.Values.image.tag }}" returns one big match as opposed to two matches within the brackets.

I have tried using (\-)?( )?(.*):( )\{\{( )(\$)?.Values.*\}\}.

I'd like to get both value instances instead of one large instance.

  • 写回答

1条回答 默认 最新

  • doudui6756 2019-08-12 18:40
    关注

    I'm guessing that maybe you might want to write an expression that'd look somewhat similar to:

    \s*{{\s*\$?\.Values([^}]*)?\s*}}
    

    Test

    package main
    
    import (
        "regexp"
        "fmt"
    )
    
    func main() {
        var re = regexp.MustCompile(`(?m)\s*{{\s*\$?\.Values([^}]*)?\s*}}`)
        var str = `image: "{{ $.Values.image.repository }}:{{ $.Values.image.tag }}`
    
        for i, match := range re.FindAllString(str, -1) {
            fmt.Println(match, "found at index", i)
        }
    }
    

    If you wish to explore/simplify/modify the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


    RegEx Circuit

    jex.im visualizes regular expressions:

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵