I am trying to remove a module out of a file containing a list of modules. My case uses verilog hdl language, however, the problem is language independent.
A sample of the file looks like this:
(* generic one *)(* generic two *)
(* generic three *)
(* generic four *)
module one
wire a;
wire b;
wire c;
endmodule;
(* generic one *)
(* generic two *)
module two
wire a;
wire b;
wire c;
endmodule;
module three
wire a;
wire b;
wire c;
endmodule;
(* generic one *)
module four
wire a;
wire b;
wire c;
endmodule;
module five
wire a;
wire b;
wire c;
endmodule;
I would like to remove module four. This implies removing the generic declaration before module four
all the way to endmodule
.
My approach is to find the last endmodule
which occurs before module four
. A random number of lines can occur between the two and those have to be removed as well. How can I find these random lines?
I tried to use positive lookbehind to solve this problem. I am using python3 regex library.
The pattern I tried to use is:
pattern = re.compile(r'(?:<=endmodule).*?module four.*?endmodule', re.DOTALL)
The problem with this pattern is (?:<=endmodule).*?
matches the first endmodule
in the file all way to module four
, rather than the last endmodule
which occurs before module four
.