kshitij kulshreshtha
kshitij kulshreshtha

Reputation: 47

TCL script to match and extract the substring carrying the regex

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

Now I am using the same code but not getting the desired output

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 *"]

But this is giving me null result , could you tell me any problem in this ?

Upvotes: 0

Views: 316

Answers (1)

Jerry
Jerry

Reputation: 71538

Well first, you are applying the regexp command to a list, which is not quite recommended. As it is right now, you might get away without using regexp, like so:

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;}

From there you can loop and extract the input list:

set input [lmap n $match {string map {";" ""} [lindex [split $n] end]}]
# a ef gh

EDIT: Regarding your edit, a regexp lsearch would be more appropriate because of the potential leading spaces:

set input [lsearch -all -inline -regexp $lines_3 {^ *input wire .*}]

Though you could use a single regexp on the whole file:

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

The above regexp uses ^ and $ which normally match the beginning and end of a string respectively, but with -lineanchor matches the beginning and end of a line in the string respectively. ([^;]+) simply 'captures' everything that's not a semicolon. -inline makes regexp return the actual matches (otherwise we get the match count) and -all makes the it look for all possible matches (and not just stop at the first match).

Upvotes: 1

Related Questions