我正在尝试替换文件中的几行。这是我的示例文件:
input a1;
//Power_off assertion for all power ports (1 per each power domain)
// `assert_Check1
//Start of functional specification here
// always @* reg_z4=Check
这是我编写的用于替换行的python代码:
import re,sys,fileinput
for line in fileinput.input(sys.argv[1], inplace=True):
assertPattern=re.match(r'\/\/\s(`assert_.+\)$)',line, re.M)
alwaysPattern=re.match(r'^\/\/\s(always\s@\*.+\;$)',line, re.M)
print (line.rstrip().replace('//Power_off assertion for all power ports (1 per each power domain)', '`ifdef assert_stdcell\n\n//Power_off assertion for all power ports (1 per each power domain)'),)
if assertPattern:
print (line.rstrip().replace(assertPattern.group(),assertPattern.group(1)))
print (line.rstrip().replace('//Start of functional specification here','`endif\n\n//Start of functional specification here'),)
if alwaysPattern:
print (line.rstrip().replace(alwaysPattern.group(),alwaysPattern.group(1)),)
此代码可以执行一些替换操作。但是,该代码复制了文件中的所有行。此外,始终无法替换atternPattern和assertPattern。这是代码的输出:
input a1;
input a1;
`ifdef assert_stdcell
//Power_off assertion for all power ports (1 per each power domain)
//Power_off assertion for all power ports (1 per each power domain)
// `assert_Check1
// `assert_Check1
//Start of functional specification here
`endif
//Start of functional specification here
// always @* reg_z4=Check
// always @* reg_z4=Check
有人知道为什么会发生重复吗?为什么图案替换无法正常工作?
答案 0 :(得分:0)
正在发生复制,因为您必须在ifs之外打印语句。因此,您将始终打印该行两次,此外,如果任何正则表达式匹配(它们不匹配),您还将再次打印该行。