【问题标题】:How to escape curly braces in Tcl (f5 bigip cli script)如何在 Tcl 中转义花括号(f5 bigip cli 脚本)
【发布时间】:2022-10-17 13:32:44
【问题描述】:

你能帮助我吗

这是情况

我有一个类似于这个的字符串

profile1 { context all } profile2 { context serverside } profile3 { context clientside } profile4 { context clientside } profile5 { context serverside }

我想更改字符串以使每个配置文件及其上下文在其自己的行上,如下所示:

profile1 { context all }
profile2 { context serverside }
profile3 { context clientside } 
profile4 { context clientside } 
profile5 { context serverside }

我正在考虑使用 regsub 将“}”替换为“}\n”

set modified_profilelist [regsub -all "string_to_replace" $profilelist "replacement_string" ]

但我找不到摆脱花括号的方法

我尝试的任何事情都会给我一个错误

提前致谢

【问题讨论】:

  • 显而易见的方法是将 string_to_replace 写为“\}” - 这不起作用吗?
  • 完全不同的做法如何:join [lmap {profile context} $str {list $profile $context}] \n

标签: command-line-interface escaping tcl f5


【解决方案1】:

我的解决方案是使用 string map 替换右花括号 + 右花括号 + 新行的空格:

set originalString "profile1 { context all } profile2 { context serverside } profile3 { context clientside } profile4 { context clientside } profile5 { context serverside }"
puts "$originalString"
puts "---"
set newString [string map { "} " "}
"}  $originalString]
puts "$newString"

输出:

profile1 { context all } profile2 { context serverside } profile3 { context clientside } profile4 { context clientside } profile5 { context serverside }
---
profile1 { context all }
profile2 { context serverside }
profile3 { context clientside }
profile4 { context clientside }
profile5 { context serverside }

【讨论】:

    【解决方案2】:

    如果您的真实数据(如示例)创建了一个有效的 tcl 列表,您可以将其视为一个,并一次遍历两个元素:

    set profiles {profile1 { context all } profile2 { context serverside } profile3 { context clientside } profile4 { context clientside } profile5 { context serverside }}                                                                           
                                                                                                                                                                                                                                                      
    foreach {name value}  $profiles {                                                                                                                                                                                                                 
        puts [list $name $value]                                                                                                                                                                                                                      
    }                                                                                                                                                                                                                                                 
    

    【讨论】:

      【解决方案3】:

      在 Tcl 中,如果没有左大括号,右大括号没有特殊含义,因此您可以安全地执行以下操作:

      regsub -all } $profilelist }
      
      

      如果你想在一个过程中使用它,那么这也可以:

      regsub -all } $profilelist }
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-17
        • 1970-01-01
        相关资源
        最近更新 更多