获取tcl中指定模式前面的元素

Get the element in front of a specified pattern in tcl

我正在编写一个脚本,它将搜索系统 Verilog 测试平台并提取模块,然后搜索模块层次结构,直到它到达链的最底部。

有什么方法可以搜索模式,然后获取指定字符串之前的元素吗?这些文件都是不同的,除了它们总是有我在这里需要的元素...

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

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

我需要 #( 之前的关键字。我一直在与 regexp 合作,但任何想法都会有很大的帮助!

# 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]

该正则表达式找到一个 "word"(字母、数字、下划线),后跟可选的空格(包括换行符)、一个井号和一个左括号。