I need to search something like this:
lines = """package p_dio_bfm is
procedure setBFMCmd (
variable pin : in tBFMCmd
);
end p_dio_bfm; -- end package;
package body p_dio_bfm is
procedure setBFMCmd (
variable pin : in tBFMCmd
) is
begin
bfm_cmd := pin;
end setBFMCmd;
end p_dio_bfm;"""
I need to extract the package name, i.e. p_dio_bfm and the package declaration, i.e. the part between "package p_dio_bfm is" and FIRST "end p_dio_bfm;"
The problem is that the package declaration may end with "end p_dio_bfm;" or "end package;" So I tried the following "OR" regex which: - works for packages ending with "end package" - does not work for packages ending with "end pck_name;"
pattern = re.compile("package\s+(\w+)\s+is(.*)end\s+(package|\1)\s*;")
match = pattern.search(lines)
The problem is the (package|\1) part of the regex, where I what to catch either the word "package" or the matched package name.
UPDATE: I have provided a full code that I hope will clarify it:
import re
lines1 = """package p_dio_bfm is
procedure setBFMCmd (
variable pin : in tBFMCmd
);
end p_dio_bfm;
package body p_dio_bfm is
procedure setBFMCmd (
variable pin : in tBFMCmd
) is
begin
bfm_cmd := pin;
end setBFMCmd;
end p_dio_bfm;"""
lines2 = """package p_dio_bfm is
procedure setBFMCmd (
variable pin : in tBFMCmd
);
end package;
package body p_dio_bfm is
procedure setBFMCmd (
variable pin : in tBFMCmd
) is
begin
bfm_cmd := pin;
end setBFMCmd;
end package;"""
lines1 = lines1.replace('\n', ' ')
print lines1
pattern = re.compile("package\s+(\w+)\s+is(.*)end\s+(package|\1)\s*;")
match = pattern.search(lines1)
print match
lines2 = lines2.replace('\n', ' ')
print lines2
match = pattern.search(lines2)
print match
I expect in both cases, using a unique regex, to get back this part:
"""procedure setBFMCmd (
variable pin : in tBFMCmd
);"""
without the \n chars which I have removed.