正则表达式匹配两个字符串之间的所有内容,跨越多行 [英] Regex match everything between two string, spaning multiline

查看:26
本文介绍了正则表达式匹配两个字符串之间的所有内容,跨越多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何正则表达式匹配两个字符串之间的所有内容?两个字符串之间的东西跨越多行,也可以包含所有的html字符.

How do I regex match everything that is between two strings? The things between two strings span several lines and can contain all html characters too.

例如:

<p>something</p>

<!-- OPTIONAL -->

<p class="sdf"> some text</p>
<p> some other text</p>

<!-- OPTIONAL END -->

<p>The end</p>

我想去掉整个可选部分.但是贪婪的任何字符匹配都没有做我想要的..我使用的模式是

I want to strip the whole optional part off. but the greedy any character match isn't doing what I wanted.. the pattern I'm using is

  • .*
  • (.*)
  • (.*)\s+
  • (?=)(.*)\s+

它们都匹配第一个可选标签,如果只给出了第一部分,但不能很好地处理完整的行.

All of them match the first optional tag, if only the first part is given, but doesn't do well with complete lines.

这是一个例子:http://regexr.com?352bk

谢谢

推荐答案

检查 RegExr 中的 dotall 复选框 :)

Check the dotall checkbox in RegExr :)

如果没有 dotall 标志(/regex/s 中的 s),点 (.) 将不匹配回车.

Without the dotall flag (the s in /regex/s), a dot (.) won't match carriage returns.

您应该使用 .*? 而不是 .* 延迟匹配可选内容(参见PLEASE DO NOT MATCH!例子).

这篇关于正则表达式匹配两个字符串之间的所有内容,跨越多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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