I thought that wasn't that hard to do, but I want to remove all empty lines (or lines just containing blanks and tabs in Java) with String.replaceAll.
My regex looks like this:
s = s.replaceAll ("^[ |\t]*\n$", "");
But it doesn't work.
I looked around, but only found regexes for removing empty lines without blanks or tabs.
There is also a very handy keyboard shortcut to delete rows (columns or cells). Press Ctrl + – on the keyboard. That's it! Our blank rows are gone now.
Microsoft Office Word does not provide a convenient way to remove empty rows and columns, and you need to remove them by manually select each empty row and column and then delete them one by one. Step 4: In the Rows & Columns group, click Delete Rows or Delete Columns.
First you need to open the file with Notepad++. Once the document is loaded, select Edit > Line Operations > Remove Empty Lines. Voilà, every single blank lines has been deleted.
Try this:
String text = "line 1\n\nline 3\n\n\nline 5"; String adjusted = text.replaceAll("(?m)^[ \t]*\r?\n", ""); // ...
Note that the regex [ |\t]
matches a space, a tab or a pipe char!
B.t.w., the regex (?m)^\s+$
would also do the trick.
I don't know the syntax for regular expressions in Java, but /^\s*$[\n\r]{1,}/gm
is the regex you're looking for.
You probably write it like this in Java:
s = s.replaceAll("(?m)^\\s*$[\n\r]{1,}", "");
I tested it with JavaScript and it works fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With