首页 > 解决方案 > 如何在 tcl 中将 \n 设置为 str

问题描述

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

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

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

谢谢!

标签: tclregsub

解决方案


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

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

印刷

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

推荐阅读