gpt4 book ai didi

regex - 当表达式具有 [0] 时,TCL regsub 不工作

转载 作者:行者123 更新时间:2023-12-02 06:43:49 26 4
gpt4 key购买 nike

我尝试了以下代码:

set exp {elem[0]}
set temp {elem[0]}

regsub $temp $exp "1" exp

if {$exp} {
puts "######### 111111111111111 ################"
} else {
puts "########### 0000000000000000 ############"
}

当然,这是最简单的 regsub 可能(单词完全匹配),但它仍然不起作用,也没有进行任何替换。如果我写 elem 而不是 elem[0],一切正常。我尝试使用 {elem[0]}、elem[0]、“elem[0]”等,但都没有用。有什么线索吗?

最佳答案

This is the easiest regsub possible (the words match completely)

实际上,不,单词不匹配。你看,在正则表达式中,方括号是有意义的。你的表达 {elem[0]} 实际上意味着:

match the sequence of letters 'e'
followed by 'l'
followed by 'e'
followed by 'm'
followed by '0' (the character for the number zero)

因此它将匹配字符串“elem0”而不是“elem[0]”,因为“m”之后的字符不是“0”。

您想要的是 {elem\[0\]} <-- 反斜杠转义特殊含义。

阅读 tcl 正则表达式语法手册,re_syntax ,了解有关正则表达式如何在 tcl 中工作的更多信息。

关于regex - 当表达式具有 [0] 时,TCL regsub 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3660155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com