RegEx 表达式将捕获两个字符之间的所有内容,包括多行块 [英] RegEx expression that will capture everything between two characters including multiline blocks

查看:21
本文介绍了RegEx 表达式将捕获两个字符之间的所有内容,包括多行块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕获所有文本 &<% 和 %> 之间的文本块.

I want to capture all text & blocks of text between <% and %>.

例如:

<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>也屏蔽.

I have tried \<\%(.*)\%\> but that will capture everything including <h1>Test Template</h1> block as well.

推荐答案

您使用的是哪个正则表达式引擎?

Which regex engine are you using?

<%(.*?)%>

应该在启用点匹配换行符"选项的情况下工作.如果您不知道如何设置,请尝试

should work with the "dot matches newline" option enabled. If you don't know how to set that, try

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

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

顺便说一下,无需转义 <%>.

No need to escape <, %, or > by the way.

这篇关于RegEx 表达式将捕获两个字符之间的所有内容,包括多行块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆