Basic Answer
If this is what you want to match:
- Multiple continuous empty lines where multiple means > 1.
- All empty lines at the end of a file except the one implicitely generated by
\n
-terminating the file (which can be considered as a good practice, see here).
- All redundant whitespaces after the terminating
\n
.
Then this pattern might help you:
(^\s*(\r|\n)){2,}|^\s+(\r|\n)?\Z
Further Explanation
The first part (^\s*(\r|\n)){2,}
takes care of 1., the second part ^\s+(\r|\n)?\Z
matches redundant empty lines at the end of a file or redundant whitespaces following the terminating \n
.
If your file looks like this (with Unix file endings) ...
1. FirstLine\n
2.
3. ThirdLine\n
4. FourthLine\n
5.
6.
7. SeventhLine\n
... then it only matches lines 5 and 6, but nothing at the end. Notepad++ though will show an 8th line at the end due to the terminating \n
. However, if there would be multiple \n
s at the end of the file or if there would be additional \t
or spaces after the terminating \n
in the 7th line, theese would match.
If you also want to match the line generated by the \n
termination (and as a result remove the \n
termination when replacing), you could as well use ^\s*\Z
instead for the second part of the regular expression.
Additional explanation of \s*(\r\n)
: This matches every allowed combination like abc\n
, abc\r\n
or abc\r
because \s
also includes \n
and \r
.
\Z
matches the end of the whole file/input (whereas $
would only match a line's end).
I'm sure there might be a shorter version of the regular expression but my first intention was to make it work and understandable.