<p>您可以将变量空格与<code>\s*</code>(零个或多个空格)或<code>\s+</code>(一个或多个空格)匹配,并且可以使用<code>(...)</code>括号“捕获”文本。你知道吗</p>
<p>查看<a href="http://www.asic-world.com/verilog/syntax2.html" rel="nofollow noreferrer">this description of the Verilog ^{<cd4>} syntax</a>,可以看到您将查找<code>input</code>,后跟一个可选范围,后跟一个或多个标识符,即<a href="http://verilog.renerta.com/source/vrg00018.htm" rel="nofollow noreferrer">delimited by whitespace</a>。以下模式将从这样的语句中捕获标识符的<em>列表</em>:</p>
<pre><code>r'^input\s+(?:\[[^\]]*\]\s+)?(.+);'
</code></pre>
<p><code>(?:\[[^\]]*\]\s+)?</code>部分将匹配可选范围语法(a<code>[</code>,后跟非<code>]</code>字符上的任何数字,后跟<code>]</code>),而不捕获它。有关在线演示,请参见<a href="https://regex101.com/r/cT0Q0X/1" rel="nofollow noreferrer">https://regex101.com/r/cT0Q0X/1</a>。你知道吗</p>
<p>因为标识符总是以空格分隔的,所以可以使用<code>str.split()</code>将捕获的值转换为Python列表。你知道吗</p>
<p>您不需要将文件读入内存或使用<code>range</code>。直接在文件上循环。而且您不需要使用<code>re.M</code>,因为您正在处理单个行。我还要删除<code>re.I</code>,因为Verilog是区分大小写的;<code>INPUT</code>与<code>input</code>不同:</p>
<pre><code>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)
</code></pre>
<p>使用您的示例演示:</p>
<pre><code>>>> 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
</code></pre>