I have a file that I need to read. The file I'm reading is as follows:
module first(x,y,z);
output x;
input y;
input [0:7] z;
# z is later used in the file and occurs more than twice
endmodule
.....
After reading the above file I need to look for any lines that follow the pattern of input identifier; or input [digit:digit] identifier; For that I have made a regular expression as follows:
pattern = re.compile(r'(input)(\s*)(\[*\d*\d*\d*:*\d*\d*\d*\]*)(\s+)(\w+)')
I then need to find the variable name (identifier). In the above example in input y; . I would need to find the y. If this variable occurs twice or less I need to remove it. So, if y occurred only twice in the file I would remove the input y line and y in the parenthesis. So the file would change to
module first(x,z);
output x;
input [0:7] z;
# z occurs more than twice in the file
endmodule
.....
The code I have written is as follows:
pattern = re.compile(r'(input)(\s*)(\[*\d*\d*\d*:*\d*\d*\d*\]*)(\s+)(\w+)')
with open('filename' ,'r') as f:
data = f.read()
result = re.search(pattern, data)
identifier = search_result.group(5)
The above code allows me to only get the first variable name in an input, in the file and that's it. I tried putting it in a for loop but that didn't work either.
After I find all the variable names declared as input I want to check if the variable name occurred twice or less in the file. If it occurs less than twice, then I would have to remove both it's declaration line as in input y and in parenthesis.
How would I go about this?
