社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

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

shanuj garg • 5 年前 • 1594 次点击  

我正在尝试生成一个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
 
1594 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Ashish kulkarni
Reply   •   1 楼
Ashish kulkarni    6 年前

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

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 是正则表达式的好教程。

Martijn Pieters
Reply   •   2 楼
Martijn Pieters    6 年前

您可以将变量空白与 \s* (零个或多个空格)或 \s+ (一个或多个空格),您可以使用 (...) 圆括号。

看着 this description of the Verilog input syntax ,你可以看到你会寻找 输入 后跟一个可选范围,后跟一个或多个标识符,它们是 delimited by whitespace . 下面的模式将捕获 列表 来自这样一个语句的标识符:

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

这个 (?:\[[^\]]*\]\s+)? 部分将匹配可选范围语法(a [ ,后跟非上的任意数字- ] 字符,后跟 ] ,但没有捕获它。见 https://regex101.com/r/cT0Q0X/1 在线演示。

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

您不需要将文件读入内存或使用 range . 直接循环文件。你不需要使用 re.M ,因为您正在处理单个行。我也会放弃 re.I ,因为verilog是区分大小写的; INPUT 不是同一件事 输入 :

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