2

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.

hkassir72
  • 135
  • 10
  • One solution is to look for the generic definitions rather than the last `endmodule`. It would look something like `r' (\(.*?\)(\n)*)*module four.*?endmodule'`. However, I'm more curious about solving the problem with non-greedy positive lookbehind. – hkassir72 Oct 07 '19 at 16:01
  • 1
    A tempered greedy token will work - `(endmodule;)(?:(?!module|module four).)*?module four.*?endmodule;` and replace with `\1`, see https://regex101.com/r/RBM8h9/2 – Wiktor Stribiżew Oct 07 '19 at 17:35

0 Answers0