我想查找特定文件中是否存在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"
}
}
答案 0 :(得分:-1)
尝试:
if { ![regexp "if *{ *[info exists $currVal] *}" $myFileContent] } {
puts "Code not present"
}
使用“在TCL正则表达式中时,使用[表示要遵循的命令/关键字。
答案 1 :(得分:-1)
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$