问题
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