1

I am writing a script that will search a system Verilog testbench and pul out the modules and then search the modules hierarchy until it reaches the very bottom of the chain.

Is there any way to search for a pattern, and then take the element that comes just BEFORE the specified string? These files are all different except they will always have the element I need here...

//random code//
element_needed #(...)

or

//random code//
element_needed
    #(....
      ....)

I need the keyword that comes just before the #(. Ive been working with regexp but any ideas would be a huge help!

Liam P
  • 217
  • 5
  • 13

1 Answers1

4
# read the whole file into a variable
set fh [open "file"]
set contents [read $fh]
close $fh

# find the desired words
set elements [regexp -inline -all {\w+(?=\s*#\()} $contents]

That regular expression finds a "word" (letters, numbers, underscore) that is followed by optional whitespace (including newlines), a pound sign and an open parenthesis.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Thank you this works for me. Regular expressions is something im really trying to get better at. – Liam P Aug 11 '15 at 14:08