tcl - 如何在 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?
谢谢!
解决方案
使用大括号而不是引号来防止评估反斜杠转义:
set a {\n\[\"this is my code\"\]}
puts $a
印刷
\n\[\"this is my code\"\]
推荐阅读
- python - python中的两级分类
- go - 管道redis是否支持集群模式的redis服务器?
- java - 骆驼路线没有在 xml 文件中得到解决
- c# - DBContext 初始化抛出 SocketException
- javascript - 鼠标流触发onbeforeunload不起作用
- ar.js - 如何将自定义图标添加到地名 AR.JS
- g++ - 在 .cpp 文件中声明了一个全局变量,但我不能在另一个 .hpp 文件中使用它
- wpf - 是否可以在 WPF 中使用 ReactiveUI 绑定仅通过 INotifyDataErrorInfo 验证用户输入?
- binding - std::is_same 在使用 C++17 结构化绑定时给出了奇怪的结果
- docker - 码头工人撰写错误:yaml.parser.ParserError:预期'
',但发现' '