Tcl[regexp]:用于匹配内部带有变量的if块的表达式

Tcl[regexp]:用于匹配内部带有变量的if块的表达式,regex,tcl,tclsh,Regex,Tcl,Tclsh,我想找出一个If块是否存在于一个特定的文件中 我有一个文件new.tcl,如下所示: if { [info exists var1] } { if { "$var1" == "val1" } { puts "var1 has value as val1" } } else { puts "var1 does not exists" } 我在另一个Tcl函数中读取了这个文件,并尝试通过regexp函数匹配if块,该函数中使用的值和变量都是变量 我的实现文件看起来像 set v

我想找出一个If块是否存在于一个特定的文件中

我有一个文件new.tcl,如下所示:

if { [info exists var1] } {
  if { "$var1" == "val1" } {
    puts "var1 has value as val1"
  }
} else {
  puts "var1 does not exists"
}
我在另一个Tcl函数中读取了这个文件,并尝试通过
regexp
函数匹配if块,该函数中使用的值和变量都是变量

我的实现文件看起来像

set valueDict [list 'var1' 'val1']
set valDictLen [llength $valueDict]
set myFilePtr [open "new.tcl" "r"]
set myFileContent [read $myFilePtr]
close $myFilePtr

for { set index 0 } { $index < $valDictLen } { incr index 2 } {
  set currVar [lindex $valueDict $index]
  set currVal [lindex $valueDict [expr $index + 1]]

  # I actually want to match the entire if block content here
  if { ![regexp "if \{ \[info exists $currVal\] \}" $myFileContent] } {
    puts "Code not present"
  }
}
set valueDict[列表'var1'val1']
设置值dictlen[llength$valueDict]
设置myFilePtr[打开“new.tcl”“r”]
设置myFileContent[读取$myFilePtr]
关闭$myFilePtr
对于{set index 0}{$index<$valDictLen}{incr index 2}{
设置currVar[lindex$valueDict$index]
设置currVal[lindex$valueDict[expr$index+1]]
#实际上,我想在这里匹配整个if块内容
如果{![regexp”如果\{\[info exists$currVal\]\}“$myFileContent]}{
放入“代码不存在”
}
}
试试:


当您在TCL regexp中使用“时,使用[表示要遵循的命令/关键字。

如果代码的格式很好,您可以使用regex,例如(在TCL中未测试),但我怀疑它在所有情况下都能起作用。使用正则表达式解析代码通常是个坏主意。请注意,我想在正则表达式中使用变量
$currVal
,而不是
var1
,直接查看此答案并告诉我它是否解决了您的问题:不,这里的主要问题是它应该匹配
{}[]
并且它还需要用双引号(“)括起来因为它包含一个在求值时需要替换的变量,这听起来确实像是XY问题。请后退一步,放下正则表达式,告诉我们为什么要为该语句搜索一些代码?嗨,Bordoloi,这个解决方案不起作用。它实际求值表达式并替换正则表达式中的值lar表达式您使用的是哪个tcl版本?我使用的是tcl 8.5。7@Mr.Bordoloi,您需要转义双引号中的方括号。您可能不同意。请看下面的代码段。我在另一个答案中添加了。请检查您的代码是否符合要求。模式字符串的计算结果为1,因此它与任何包含至少一个1字符的字符串匹配。为什么不使用模式“info exists$currVal”?请添加puts$myFileContent。它仍在打印字符串,而不是已计算的字符串。请注意,我使用了{}而不是“我在谈论模式,即,
”[info exists$currVal]”
。当调用
regexp
命令时,它的计算结果为1,这意味着真正的命令行是
regexp 1[info exists var1]
@PeterLewerin:你是对的。谢谢。将“[info exists$currVal]”更改为{[info exists$currVal]}解决了这个问题。这也不起作用,请尝试使用“info exists$currVal”。(并花一些时间熟悉指挥评估。)
SBORDOLO-M-V1VG:Downloads sbordolo$ cat t3

set var1 "value"
set currVal "var1"
#set myFileContent "\[info exists var1\]"
set myFileContent {[info exists var1]}

if { ![regexp "[info exists $currVal]" $myFileContent] } {
    puts "Code not present"
} else {
    puts "Code present"
}
SBORDOLO-M-V1VG:Downloads sbordolo$ 
SBORDOLO-M-V1VG:Downloads sbordolo$ tclsh t3
Code present
SBORDOLO-M-V1VG:Downloads sbordolo$ 
SBORDOLO-M-V1VG:Downloads sbordolo$ cat t3

set var1 "value"
set currVal "var1"
#set myFileContent "\[info exists var1\]"
set myFileContent {[info exists var1]}

if { ![regexp "[info exists $currVal]" $myFileContent] } {
    puts "Code not present"
} else {
    puts "Code present"
}
SBORDOLO-M-V1VG:Downloads sbordolo$ 
SBORDOLO-M-V1VG:Downloads sbordolo$ tclsh t3
Code present
SBORDOLO-M-V1VG:Downloads sbordolo$