开发者

Regex not matching text with newlines in Notepad++

开发者 https://www.devze.com 2023-01-17 17:56 出处:网络
I\'m trying to match <!-- Start Comment content spanning several lines here End Comment --> And I figured something like this would do the trick:

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?


See Finding line beginning using regular expression

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


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.


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 -->)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号