我正在尝试生成一个python脚本,它应该逐行读取verilog代码,当遇到“input some_name;”时,它应该匹配行并返回名称,这样我就可以计算我在verilog代码中定义的所有输入端口(verilog代码非常大)。 所以verilog代码是这样的
module(a,b,c,d, vbg
`ifdef USE_GOOD_PIN
, vb, vc, vd, vg ..... some more input and outputs
`endif
);
input [7:0] t_d;
input srd;
output comb;
output src;
inout [1:0] Iout;
output a_in;
output b_in;
input ff_parity;
我试图匹配的代码('input[7:0]t\u d;''input srd;'etc)是
import re
file = open(r'D:/pyfile/verilog.v' ,"r")
lines = file.readlines()
print(len(lines))
for i in range(0,len(lines)):
print(lines[i],end = '')
match = re.match(r'input (.*)',lines[i], re.M|re.I)
if (match):
print(match.group(1))
else:
print("nomatch")
同样,在“input”和“[]”以及“name”之间可以有一个或多个空格,因此如何使用python regex准确地获得像“t\u d”或“srd”这样的名称。你知道吗
用我写的代码,我无法符合要求。你知道吗
下面的代码更改应该可以做到这一点。你知道吗
“\s”字符序列与空格匹配。This是一个很好的正则表达式教程。你知道吗
您可以将变量空格与
\s*
(零个或多个空格)或\s+
(一个或多个空格)匹配,并且可以使用(...)
括号“捕获”文本。你知道吗查看this description of the Verilog ^{} syntax ,可以看到您将查找
input
,后跟一个可选范围,后跟一个或多个标识符,即delimited by whitespace。以下模式将从这样的语句中捕获标识符的列表:(?:\[[^\]]*\]\s+)?
部分将匹配可选范围语法(a[
,后跟非]
字符上的任何数字,后跟]
),而不捕获它。有关在线演示,请参见https://regex101.com/r/cT0Q0X/1。你知道吗因为标识符总是以空格分隔的,所以可以使用
str.split()
将捕获的值转换为Python列表。你知道吗您不需要将文件读入内存或使用
range
。直接在文件上循环。而且您不需要使用re.M
,因为您正在处理单个行。我还要删除re.I
,因为Verilog是区分大小写的;INPUT
与input
不同:使用您的示例演示:
相关问题 更多 >
编程相关推荐