如何在python中解决这个regex问题

2024-04-29 15:04:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试生成一个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”这样的名称。你知道吗

用我写的代码,我无法符合要求。你知道吗


Tags: 代码nameinre名称inputoutputlen
2条回答

下面的代码更改应该可以做到这一点。你知道吗

match = re.match(r'input\s+(.*)\s*(.*)',lines[i], re.M|re.I)
if (match):
    matches = list(match.groups())
    if '' in matches:
        matches.remove('')
    print matches[-1]
else:
    print 'nomatch'

“\s”字符序列与空格匹配。This是一个很好的正则表达式教程。你知道吗

您可以将变量空格与\s*(零个或多个空格)或\s+(一个或多个空格)匹配,并且可以使用(...)括号“捕获”文本。你知道吗

查看this description of the Verilog ^{} syntax,可以看到您将查找input,后跟一个可选范围,后跟一个或多个标识符,即delimited by whitespace。以下模式将从这样的语句中捕获标识符的列表

r'^input\s+(?:\[[^\]]*\]\s+)?(.+);'

(?:\[[^\]]*\]\s+)?部分将匹配可选范围语法(a[,后跟非]字符上的任何数字,后跟]),而不捕获它。有关在线演示,请参见https://regex101.com/r/cT0Q0X/1。你知道吗

因为标识符总是以空格分隔的,所以可以使用str.split()将捕获的值转换为Python列表。你知道吗

您不需要将文件读入内存或使用range。直接在文件上循环。而且您不需要使用re.M,因为您正在处理单个行。我还要删除re.I,因为Verilog是区分大小写的;INPUTinput不同:

with open(r'D:/pyfile/verilog.v') as file:
    for line in file:
        match = re.search(r'^input\s+(?:\[[^\]]*\]\s+)?(.+);', line)
        if match:
            identifiers = match.group(1).split()
            print(*identifiers)

使用您的示例演示:

>>> import re
>>> from io import StringIO
>>> sample = '''\
... 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;
... '''
>>> with StringIO(sample) as file:
...     for line in file:
...         match = re.search(r'^input\s+(?:\[[^\]]*\]\s+)?(.+);', line)
...         if match:
...             identifiers = match.group(1).split()
...             print(*identifiers)
...
t_d
srd
ff_parity

相关问题 更多 >