如何匹配两个字符串多行之间的所有文本 [英] How to match all text between two strings multiline
问题描述
我正在尝试完成与此处相同的事情:
I'm trying to accomplish the same thing as seen here:
即假设你有这样的文字:
i.e. assuming you have a text like:
<p>something</p>
<!-- OPTIONAL -->
<p class="sdf"> some text</p>
<p> some other text</p>
<!-- OPTIONAL END -->
<p>The end</p>
匹配的正则表达式是什么:
What is the regex that would match:
<p class="sdf"> some text</p>
<p> some other text</p>
我已经设置了一个现场测试,使用:
I've setup a live test here using:
<!-- OPTIONAL -->(.*?)<!-- OPTIONAL END -->
但它没有正确匹配.此外,页面上接受的答案对我不起作用.我错过了什么?
but it's not matching correctly. Also the accepted answer on the page didn't work for me. What am I missing?
推荐答案
不幸的是,RegExr 依赖于 JSRegExp 实现,它不支持启用您需要的标志/修饰符 的选项.
Well unfortunately, RegExr is dependent on the JS RegExp implementation, which does not support the option to enable the flag/modifier that you need.
您正在寻找 s
(DotAll) 修饰符强制点 .
以匹配换行符序列.
You are looking for the s
(DotAll) modifier forcing the dot .
to match newline sequences.
如果您使用的是 JavaScript,则可以使用以下解决方法:
If you are using JavaScript, you can use this workaround:
/<!-- OPTIONAL -->([\S\s]*?)<!-- OPTIONAL END -->/
这篇关于如何匹配两个字符串多行之间的所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!