TCL脚本来匹配和提取包含正则表达式的子字符串
现在我使用相同的代码,但没有得到所需的输出TCL脚本来匹配和提取包含正则表达式的子字符串,tcl,Tcl,现在我使用相同的代码,但没有得到所需的输出 module bist ( output wire a, output wire b, output wire c, input wire a, input wire ben, input wire kite, input mb ); assign a.kk = ak; assign b.hs =jsj; assign oo = jj; assign ltest = ll; endmodule 但是这给了我一个空
module bist
(
output wire a,
output wire b,
output wire c,
input wire a,
input wire ben,
input wire kite,
input mb
);
assign a.kk = ak;
assign b.hs =jsj;
assign oo = jj;
assign ltest = ll;
endmodule
但是这给了我一个空的结果,你能告诉我这方面有什么问题吗?首先,你正在对一个列表应用regexp命令,这是不推荐的。就目前而言,您可能不使用regexp就可以脱身,如下所示:
set filename_reverse_mapper "mod1.v"
# Read in the data; this is good even for pretty large files
set f_3 [open $filename_reverse_mapper]
set lines_3 [split [read $f_3] "\n"]
close $f_3
set match [lsearch -all -inline $lines_3 "input wire *"]
您可以在此循环并提取输入列表:
set filename_reverse_mapper "sram.sv"
# Read in the data; this is good even for pretty large files
set f_3 [open $filename_reverse_mapper]
set lines_3 [split [read $f_3] "\n"]
close $f_3
set match [lsearch -all -inline $f_3 "input wire *"]
# {input wire a;} {input wire ef;} {input wire gh;}
编辑:关于您的编辑,regexp lsearch更合适,因为潜在的前导空格:
set input [lmap n $match {string map {";" ""} [lindex [split $n] end]}]
# a ef gh
尽管您可以在整个文件上使用单个regexp:
set input [lsearch -all -inline -regexp $lines_3 {^ *input wire .*}]
上面的regexp使用^和$,它们通常分别匹配字符串的开头和结尾,但使用-lineanchor则分别匹配字符串中行的开头和结尾。[^;]+简单地“捕获”所有不是分号的内容-inline使regexp返回实际匹配项,否则我们会得到匹配计数,-all使它查找所有可能的匹配项,而不只是在第一次匹配时停止。在这种情况下,我将使用单个foreach循环,而不是一对lmap。我觉得这样更自然。既然你提到了,是的,我应该在那里使用foreach循环。我在路上的某个地方被单独的输入所吸引。请花点时间阅读。
set filename_reverse_mapper "sram.sv"
# Read in the data; this is good even for pretty large files
set f_3 [open $filename_reverse_mapper]
set lines_3 [read $f_3]
close $f_3
set allMatches [regexp -inline -all -lineanchor {^(?:in|out)put wire ([^;]+);$} $lines_3]
# {input wire a;} a {input wire ef;} ef {input wire gh;} gh {output wire c;} c {output wire d;} d {output wire fin;} fin
set inputs [lmap {m n} $allMatches {if {[string match "input*" $m]} {set n} else continue}]
# a ef gh
set outputs [lmap {m n} $allMatches {if {[string match "output*" $m]} {set n} else continue}]
# c d fin