Py学习  »  Python

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

shanuj garg • 5 年前 • 1617 次点击  

我正在尝试生成一个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_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正则表达式精确地获得't_d'或'srd'这样的名称。

我放的代码与要求不符。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43046
 
1617 次点击  
文章 [ 2 ]  |  最新文章 5 年前