代码之家  ›  专栏  ›  技术社区  ›  Lark

将捕获两个字符之间的所有内容(包括多行块)的regex表达式

  •  14
  • Lark  · 技术社区  · 15 年前

    我要捕获介于<%和%gt;之间的所有文本块。

    例如:

    <html>
    <head>
    <title>Title Here</title>
    </head>
    <body>
    <% include("/path/to/include") %>
    <h1>Test Template</h1>
    <p>Variable: <% print(second_var) %></p>
    <%
    
    variable = value;
    
    foreach(params here)
    {
        code here
    }
    
    %>
    <p><a href="/" title="Home">Home</a></p>
    </body>
    </html>
    

    我试过了 \<\%(.*)\%\> 但这将抓住一切,包括 <h1>Test Template</h1> 同时阻塞。

    3 回复  |  直到 15 年前
        1
  •  41
  •   Tim Pietzcker    15 年前

    <%(.*?)%>
    

    <%([\s\S]*?)%>
    

    (?s)<%(.*?)%>
    

    < % >

        2
  •  7
  •   Rafe Kettler    15 年前

    \<\%(.*?)\%\> .*?

    .

        3
  •  2
  •   Stijn Sanders    15 年前

    . [\u0000-\uFFFF]

    <%([\u0000-\uFFFF]*?)%>