简体   繁体   English

Notepad ++(Perl)正则表达式匹配多行模式

[英]Notepad++ ( perl ) regex match multiple line pattern

I want to remove a div from a couple hundred html files 我想从数百个html文件中删除div

<div id="mydiv">
    blahblah blah
    more blah blah
    more html
    <some javascript here too>
</div>

I thought that this would do the job but it doesn't 我以为这可以完成工作,但是没有

<div(.*)</div>

Does anyone know which is the proper regex for this? 有谁知道正确的正则表达式吗?

Regex 正则表达式

<div[^>]+>(.*?)</div>

Don't forget to check the option . matches newline 不要忘记检查该选项. matches newline . matches newline like in the image below : . matches newline下图所示的. matches newline 点匹配换行符复选框-Notepad ++ v6.2.3

Alternatively, you can use this regex also: <div[^>]+>([\\s\\S]*?)</div> with or without the checkbox checked. 另外,您也可以使用此正则表达式: <div[^>]+>([\\s\\S]*?)</div>选中或不选中该复选框。

Discussion 讨论

Since * metacharacter is greedy, you need to tell him to take as few as possible characters (use of ? ). 由于*元字符是贪婪的,因此您需要告诉他使用尽可能少的字符(使用? )。

Check that the divs you want to remove DO NOT contain nested div. 检查要删除的div,不要包含嵌套的div。 In that case, the regex at the start of my answer won't help you. 在这种情况下,我回答开头的正则表达式将无济于事。

If you face this case, I'd suggest you using an html parser. 如果您遇到这种情况,建议您使用html解析器。

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

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