Bart Kiers's answer is missing the edge case where the last line of the string is empty or contains whitespaces.
If you try
String text = "line 1\n\nline 3\n\n\nline 5\n "; // <-- Mind the \n plus space at the end!
String adjusted = text.replaceAll("(?m)^[ \t]*\r?\n", "");
you'll get a String that equals this
"line 1\nline 3\nline 5\n " // <-- MIND the \n plus space at the end!
as result.
I expanded Bart Kiers' answer to also cover this case.
My regex pattern is:
String pattern = "(?m)^\\s*\\r?\\n|\\r?\\n\\s*(?!.*\\r?\\n)";
A little explanation:
The first part of the pattern is basically the same as Bart Kiers'. It is fine, but it does not remove an "empty" last line or a last line containing whitespaces.
That is because a last line containing just whitespaces does not end with \\r?\\n
and would therefore not be matched/replaced. We need something to express this edge case. That's where the second part (after the |
) comes in.
It uses a regular expression speciality: negative lookahead. That's the (?!.*\\r?\\n)
part of the pattern. (?!
marks the beginning of the lookahead. You could read it as: Match the regular expression before the lookahead if it is not followed by whatever is defined as string that must not follow. In our case: not any character (zero or more times) followed by a carriage-return (0 or 1 times) and a newline: .*\\r?\\n
. The )
closes the lookahead. The lookahead itself is not part of the match.
If I execute the following code snippet:
String pattern = "(?m)^\\s*\\r?\\n|\\r?\\n\\s*(?!.*\\r?\\n)";
String replacement = "";
String inputString =
"\n" +
"Line 2 - above line is empty without spaces\n" +
"Line 3 - next is empty without whitespaces\n" +
"\n" +
"Line 5 - next line is with whitespaces\n" +
" \n" +
"Line 7 - next 2 lines are \"empty\". First one with whitespaces.\n" +
" \r\n" +
"\n" +
"Line 10 - 3 empty lines follow. The 2nd one with whitespaces in it. One whitespace at the end of this line " +
"\n" +
" \n" +
"\n";
String ajdustedString = inputString.replaceAll(pattern, replacement);
System.out.println("inputString:");
System.out.println("+----");
System.out.println(inputString);
System.out.println("----+");
System.out.println("ajdustedString:");
System.out.println("+----");
System.out.print(ajdustedString); //MIND the "print" instead of "println"
System.out.println("|EOS"); //String to clearly mark the _E_nd _O_f the adjusted_S_tring
System.out.println("----+");
I get:
inputString:
+----
Line 2 - above line is empty without spaces
Line 3 - next is empty without whitespaces
Line 5 - next line is with whitespaces
Line 7 - next 2 lines are "empty". First one with whitespaces.
Line 10 - 3 empty lines follow. The 2nd one with whitespaces in it. One whitespace at the end of this line
----+
ajdustedString:
+----
Line 2 - above line is empty without spaces
Line 3 - next is empty without whitespaces
Line 5 - next line is with whitespaces
Line 7 - next 2 lines are "empty". First one with whitespaces.
Line 10 - 3 empty lines follow. The 2nd one with whitespaces in it. One whitespace at the end of this line |EOS
----+
If you want to learn more about lookahead/lookbehind see Regex Tutorial - Lookahead and Lookbehind Zero-Length Assertions: