python backreference regex -
i need search this:
lines = """package p_dio_bfm procedure setbfmcmd ( variable pin : in tbfmcmd ); end p_dio_bfm; -- end package; package body p_dio_bfm procedure setbfmcmd ( variable pin : in tbfmcmd ) begin bfm_cmd := pin; end setbfmcmd; end p_dio_bfm;""" i need extract package name, i.e. p_dio_bfm , package declaration, i.e. part between "package p_dio_bfm is" , first "end p_dio_bfm;"
the problem package declaration may end "end p_dio_bfm;" or "end package;" tried following "or" regex which: - works packages ending "end package" - not work packages ending "end pck_name;"
pattern = re.compile("package\s+(\w+)\s+is(.*)end\s+(package|\1)\s*;") match = pattern.search(lines) the problem (package|\1) part of regex, catch either word "package" or matched package name.
update: have provided full code hope clarify it:
import re lines1 = """package p_dio_bfm procedure setbfmcmd ( variable pin : in tbfmcmd ); end p_dio_bfm; package body p_dio_bfm procedure setbfmcmd ( variable pin : in tbfmcmd ) begin bfm_cmd := pin; end setbfmcmd; end p_dio_bfm;""" lines2 = """package p_dio_bfm procedure setbfmcmd ( variable pin : in tbfmcmd ); end package; package body p_dio_bfm procedure setbfmcmd ( variable pin : in tbfmcmd ) 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 unique regex, part:
"""procedure setbfmcmd ( variable pin : in tbfmcmd );""" without \n chars have removed.
how about:
>>> row in re.findall( ... r'package(?:\s.*?)(?p<needle>[^\s]+)\s+is\s+(.*?)end\s+(?:package|(?p=needle));', ... lines, ... re.s ... ): ... print '{{{', row[1], '}}}' ... {{{ procedure setbfmcmd ( variable pin : in tbfmcmd ); }}} {{{ procedure setbfmcmd ( variable pin : in tbfmcmd ) begin bfm_cmd := pin; end setbfmcmd; }}} i took liberty not filter how @mihai-hangiu asked including second block.
Comments
Post a Comment