如何捕获两个标签之间的多行正则表达式? [英] How to capture multiline regex between two tags?
问题描述
选择 2 个评论标签之间的所有文本的最佳方法是什么?例如
What's the best way to select all text between 2 comment tags? E.g.
<!-- Text 1
Text 2
Text 3
-->
<\!--.*
将捕获
<\!--.*
will capture <!-- Text 1
but not Text 2
, Text 3
, or -->
编辑根据 Basti M 的回答, <\!--((?:.*\n)*)-->
将选择 first 之间的所有内容
.IE.下面的第 1 到 11 行.
Edit
As per Basti M's answer, <\!--((?:.*\n)*)-->
will select everything between the first <!--
and last -->
. I.e. lines 1 to 11 below.
我将如何修改它以仅选择单独标签中的行?即第 1 到 4 行:
How would I modify this to select just lines within separate tags? i.e. lines 1 to 4:
1 <!-- Text 1 //First
2 Text 2
3 Text 3
4 -->
5
6 More text
7
8 <!-- Text 4
9 Text 5
10 Text 6
11 --> //Last
推荐答案
根据您的底层引擎使用 s
-modifier(并在你的表达结束了.
这将使 .
也匹配换行符.
Depending on your underlying engine use the s
-modifier (and add -->
at the end of your expression.
This will make the .
match newline-characters aswell.
如果 s
-flag 对您不可用,您可以使用
If the s
-flag is not available to you, you may use
<!--((?:.*\r?\n?)*)-->
说明:
<!-- #start of comment
( #start of capturing group
(?: #start of non-capturing group
.*\r?\n? #match every character including a line-break
)* #end of non-capturing group, repeated between zero and unlimited times
) #end of capturing group
--> #end of comment
要匹配多个评论块,您可以使用
To match multiple comment blocks you can use
/(?:<!--((?:.*?\r?\n?)*)-->)+/g
这篇关于如何捕获两个标签之间的多行正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!