Ifブロックが特定のファイル内にあるかどうかを調べたいと思います。Tcl [regexp]:変数が内部にあるブロックと一致する場合の式
次のように私は、ファイルnew.tclを持っている:
if { [info exists var1] } {
if { "$var1" == "val1" } {
puts "var1 has value as val1"
}
} else {
puts "var1 does not exists"
}
私は別のTcl関数でこのファイルを読み込み、regexp
機能と、この機能で使用される値と変数を介しあればブロックを一致させよう変数です。私の実装ファイルがどのように見える
、
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"
}
}
\ s *?{\ s *?\ [info exists var1 \ s *?] \ s *のような正規表現を使用すると、 ''(?:\ n | ^) } \ s *?{。*?\ n}(?:\ n | $) '](https://regex101.com/r/PUpBvb/1)(Tclではテストされていません)、すべてのケースでうまくいくとは思えません。正規表現でコードを解析することは、一般的には悪い考えです。 –
注意変数 '$ currVal'を' var1'の代わりに正規表現の中で使いたいのですが – Ashwin
この答えを見て、それがあなたの問題を解決するかどうか教えてください:http://stackoverflow.com/questions/14237376/how-to-use-regexp-to-match-a-tcl – user2141046