python正则表达式搜索直到括号匹配

时间:2016-05-07 08:15:46

标签: python regex

我需要从像这样的字符串中提取一些数据(VHDL代码):

entBody = """entity pci_bfm is                                                            
  generic(                                                                   
    G_INST_NAME            : string          := "PCI_BFM";                   
    G_HANDLE_NO            : rpciBfmHandleNo := 0;                           
    G_IDSEL_POS_EXT_TARGET : idsel_pos       := 30;                          
    G_IDSEL_POS_INT_TARGET : idsel_pos       := 29                           
    );                                                                       
  port(                                                                      
    i_tb_stop  : in    boolean;                       -- Testbench global sto
    o_clk      : out   std_logic;                     -- PCI clock.          
    o_rstn     : out   std_logic;                     -- PCI reset.          
    o_idsel    : out   std_logic;                     -- Initialization devic
    i_reqn     : in    std_logic;                     -- Request. The reqn in
    o_gntn     : out   std_logic;                     -- Grant. The gntn onpu
    io_ad      : inout std_logic_vector(31 downto 0); -- Address/data bus. Th
    io_cben    : inout std_logic_vector(3 downto 0);  -- Command/byte enable.
    io_par     : inout std_logic;                     -- Parity. The par sign
    io_framen  : inout std_logic;                     -- Frame. The framen si
    io_irdyn   : inout std_logic;                     -- Initiator ready. The
    io_devseln : inout std_logic;                     -- Device select. Targe
    io_trdyn   : inout std_logic;                     -- Target ready. The tr
    io_stopn   : inout std_logic;                     -- Stop. The stopn sign
    io_perrn   : inout std_logic;                     -- Parity error. The pe
    i_serrn    : in    std_logic;                     -- System error. The se
    i_intan    : in    std_logic;                     -- Interrupt A. The int
    o_lockn    : out   std_logic                      -- Locked operations. R
    );                                                                       
end entity pci_bfm;"""

VHDL注释的大小不一样,我将它们截断为更容易阅读。

我有兴趣在'port('和last')之间获取所有内容;' (关闭端口声明的那个)。当然,VHDL声明可能不会像这里那样缩进和格式化。

我有一个Python 2.7.x正则表达式:

pattern = re.compile("port\s*\((.*?)\s+\)\s*;")
match3 = pattern.search(entBody)
ports = match3.group(1)

如果结束则效果很好);并不是在最后一次声明之后。以下内容不起作用:

entBody2 = """entity QSPI_FLASH_SPANSION_S25FL_BFM is
  generic
    (
      G_INST_NAME : string  := "QSPI_FLASH_SPANSION_S25FL_BFM";
      G_HANDLE_NO : integer := 2
      );
  port (
    tb_stop : in    boolean;                       -- Testbench global stop.
    sclk    : in    std_logic;
    csn     : in    std_logic;
    sdat    : inout std_logic_vector(3 downto 0));
end;"""

如果我改变我的正则表达式有点像这样:

pattern = re.compile("port\s*\((.*?)\s*\)\s*;") # \s* instead of \s+

然后搜索结束于'io_ad:inout std_logic_vector(31 downto 0',这一点都不好。

我想知道我是否可以使用正则表达式来进行这样的搜索,即计算左括号并仅在所有括号都关闭时停止。

如果没有简单的方法,我将使用字符串函数进行简单的字符串搜索来解决它。

谢谢。

2 个答案:

答案 0 :(得分:0)

在这里你要匹配包括换行符在内的字符。因此,在字符类中使用模式\s\S

\s匹配任何空格字符。

\S匹配任何非空白字符

match3 =re.search(r"port\(([\s\S]+?)\);\s+\n",entBody)

S标志。帮助匹配任何角色,包括换行符。

match3 =re.search(r"port\((.+?)\);\s+\n",entBody,re.S)

答案 1 :(得分:0)

您可以使用以下正则表达式:

/port\s*\((.+)\)\s*;/s

打破它:

port            # matches the characters port literally (case sensitive)
\s*             # match any white space character [\r\n\t\f ] Between zero and unlimited times
\(              # matches the character ( literally
(.+)            # capturing group start - matching any character - Between one and unlimited times
\)              # matches the character ) literally
\s*             # match any white space character [\r\n\t\f ] Between zero and unlimited times
;               # matches the character ; literally

s               # modifier: single line. Dot matches newline characters

REGEX DEMO

IDEONE DEMO

更新:如果在port(...)之后还有其他内容,您可以检查以下正则表达式:

/port\s*\((.*?)(?:\)\s*;\s*\w)/s