Regex not matching text with newlines in Notepad++

随声附和 提交于 2021-02-11 16:58:43

问题


I'm trying to match

<!-- Start Comment

content spanning several lines here

End Comment -->

And I figured something like this would do the trick:

(<!-- Start Comment).*(End Comment -->)

but the . is not matching newlines. How do I get it to recognize my entire block which contains a host of different characters including newlines?


回答1:


It doesn't seem that Notepad++ handles newlines very well. This page has some creative workarounds, though:

http://www.powercram.com/2009/08/notepad-guide-to-using-regular.html




回答2:


See Finding line beginning using regular expression

Apparently, Notepad++ regular expressions are line-based. They can't span lines.




回答3:


Actually I've managed to make this work using something like this:

(?s)BEGINNING-TEXT(.*)FINAL-TEXT

This works in both Notepad++ and Sublime Text.




回答4:


The Simple Way

Just check ". matches newline" in Notepad++ before you do the search. That's it.

Note: In a more complex example, if you had other dots in your regex that you didn't want to match to newlines, you could replace them with [^\r\n], meaning "match anything that's not a newline".

The Complex Way

You may not want to use the ". matches newline" setting. Maybe it's not your thing. In that case, there's another way, but it's a bit hacky. Read on.

Now that Notepad++ has an OR operator, we can use that to search for any character, including newlines, and interchangeably in the same regex have dots that match non-new line characters too. This also means we don't have to check the ". matches newline" checkbox either, which is nice. How to do it is to use the following regex instead of .:

(?:\s|.)*

What that says is "match a dot or any of the whitespace character, including newlines". The ?: inside the brackets tells Notepad++ not to capture this group.

So for the above example, we can find it with:

(<!-- Start Comment)(?:\s|.)*(End Comment -->)

If you wanted to capture the in-between bit, including whitespace, add an extra pair of brackets like this:

(<!-- Start Comment)((?:\s|.)*)(End Comment -->)


来源:https://stackoverflow.com/questions/3791240/regex-not-matching-text-with-newlines-in-notepad

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!