如何在 tcl 中将 \n 设置为 str

How to set \n to a str in tcl

您好,我是 tcl 的新手,想将“\n”分配给一个变量,并在 regsub 中使用该变量来替换该字符串。 它应该像:

set a "\n\[\"this is my code\"\]"
puts $a

我期待这会给 \n\[\"this is my code\"\],然后我可以在 regsub {$a} $str "replace" sub_str 中使用这个 $a。 这个 regsub 可以在 $str 中搜索 $a 并将匹配的替换为 replace 并将其存储在 sub_str 中。 然而,它给了我 [this is my code] 有没有办法让第一种格式为 \n\[\"this is my code\"\],这样我就可以用它来做字符串 regsub?

谢谢!

使用大括号而不是引号来防止计算反斜杠转义:

set a {\n\[\"this is my code\"\]}
puts $a

打印

\n\[\"this is my code\"\]