简体   繁体   English

跨行反转正则表达式匹配

[英]Invert regex match across lines

How would you invert this expression to match everything BUT the contents between the <!-- LIST --> and <!-- /LIST --> tags? 您将如何反转该表达式以匹配所有内容,但要匹配<!-- LIST --><!-- /LIST -->标记之间的内容?

((?s)<!-- LIST -->.*?<!-- /LIST -->)

Meaning I'd like to remove everything before <!-- LIST --> and after <!-- /LIST --> 意思是我想删除<!-- LIST --><!-- /LIST -->

The regex you have used already matches the section between the two tags, you have to simply add the prior and following sections and use the backreference to replace all the contents with the saved group (usually the slash / is to escape also). 您使用的正则表达式已经与两个标签之间的部分匹配,您只需简单地添加前面和后面的部分,并使用向后引用将所有内容替换为已保存的组(通常,斜杠/也要转义)。

This is a generic regex code: 这是一个通用的正则表达式代码:

s/(?s).*(<!-- LIST -->.*?<!-- \/LIST -->).*/\1/

Implementation online here 在此处在线实施

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM