tcl 字符串替换 [英] tcl string replacement
问题描述
$arg=TEST #### Requested, NOT AVAILABLE psy #;
我上面有一个字符串,其中 # 是动态生成的.我必须在 tcl 中使用一个函数来进行字符串替换.基本上我需要从上面的表达式中删除逗号(,)并将其显示为
I have a string above where the # is dynamically generated. I have to use a function in tcl to do a string replacement. Basically I need to remove the comma(,) form the above expression and display it as
测试 #### 请求不可用 psy #
TEST #### Requested NOT AVAILABLE psy #
这是我所做的,但它不起作用.
Here's what I did, but it is not working.
regsub -all {"Requested,"} $arg {"Requested"} arg
这是我引用该函数的地方:http://www.tcl.tk/man/tcl8.5/TclCmd/regsub.htm
This is where i referenced the function from: http://www.tcl.tk/man/tcl8.5/TclCmd/regsub.htm
推荐答案
问题在于您的引用.您实际上是在检查字符串 "Requested,"
(包括引号),但这不是您想要的.尝试去掉花括号 (}
) 或双引号 ("
):
The problem is your quoting. You are actually checking for the string "Requested,"
(including the quotes), but that isn't what you want. Try either getting rid of the squiggly-brackets (}
) or double quotes ("
):
set arg "TEST #### Requested, NOT AVAILABLE psy #;"
regsub -all "Requested," $arg "Requested" arg
如果您只需要去掉逗号,您可以搜索/替换它(只需将其替换为空字符串 ""
):
If all you need to get rid of is the comma, you can search/replace that (just replace it with the empty string ""
):
regsub -all "," $arg "" arg
或
regsub -all {,} $arg {} arg
正如评论者提到的,后者在一般情况下可能更好,因为正则表达式通常包含许多反斜杠 (/
) 并且 {}
括号不t 需要大量分散注意力的额外反斜杠转义,就像 ""
引号所做的那样.
As a commenter had mentioned, the latter may be better in the general case, since regular expressions often contain many backslashes (/
) and the {}
brackets don't require massive amounts of distracting extra backslash escapes the same way that ""
quotes do.
这篇关于tcl 字符串替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!