行缺少右大括号错误
set text [join $text n]
我的全部代码是
proc ProcessText { text} {
regsub -all -- ({) $text {{} text
set text [join $text n]
return $text
}
##it starts from here
set text "{a b c"
puts $text
puts [ProcessText $text]
如果我使用regsub将{替换为任何不会引发错误的适当替换,则我会出错执行"proc ProcessText{}"时缺少右大括号
如果我评论regsub,那么我会出错"执行时列表中的大括号不匹配"加入$text\n"有人能在这里建议我如何在tcl中进行同样的操作吗。
FYI:text是一个包含大量文本信息的列表,如果我删除{,{也在其中。它在其他方面不起作用。
正如Donal已经感觉到的那样,变量text
所持有的值的格式不符合Tcl列表,这是[join]
所期望的。
您的选择是:
1( 使用[split]
:将值转换为Tcl列表
join [split $text] n
2( 使用[string map]
:避免转换为列表和[join]
string map {" " "n"} $text
(或者使用[regsub]
如下,如果您不能控制输入中的空白区扩散(
有时,一根绳子最好只是一根绳子;(
Varia
您对[regsub]
的使用是有问题的,最重要的是,最好一次性使用它来获得最终目标,而不是在调用[join]
:之前对输入字符串进行清理
regsub -all {s+} $text "n"
背景
您遇到错误是因为您没有正确地将正则表达式({)
中的sentinel{
转义为[regsub]
:
regsub -all -- ({) $text {{} text
这应该是:
regsub -all -- {{} $text {{} text
在您的变体中,{
被认为是一个大括号,实际上在脚本的其余部分中并不匹配。