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

2024-04-19 05:27:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从这样的字符串中提取一些数据(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正则表达式:

^{pr2}$

如果在最后一次声明之后没有立即结束),则效果很好。以下操作无效:

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降为0)结束,这一点都不好。在

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

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

谢谢。在


Tags: the字符串inioportoutentitystd
2条回答

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

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

分解:

^{pr2}$

REGEX DEMO

IDEONE DEMO


更新:如果在port(...)之后有其他内容,则可以检查以下regex:

^{}

这里要匹配的字符包括换行符。所以在字符类中使用模式\s\S。在

\s匹配任何空白字符。在

\S匹配任何非空白字符

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

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

^{pr2}$

相关问题 更多 >