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!
EDIT
B.t.w., the regex (?m)^\s+$
would also do the trick.
More Related Contents:
- How to split a string if any chracter is found in java
- Java: splitting a comma-separated string but ignoring commas in quotes
- Splitting a Java String by the pipe symbol using split(“|”)
- Split string to equal length substrings in Java
- Split string with dot as delimiter
- Splitting on comma outside quotes
- Java string split with “.” (dot) [duplicate]
- Java how to replace 2 or more spaces with single space in string and delete leading and trailing spaces
- How to split a string between letters and digits (or between digits and letters)?
- How can non-ASCII characters be removed from a string?
- How to split String with some separator but without removing that separator in Java? [duplicate]
- Get the index of a pattern in a string using regex
- Compare one String with multiple values in one expression
- Replace the last part of a string
- Why String.replaceAll() in java requires 4 slashes “\\\\” in regex to actually replace “\”?
- Java: Split string when an uppercase letter is found
- How to Split a mathematical expression on operators as delimiters, while keeping them in the result?
- How to replace ” \ ” with ” \\ ” in java
- Validate a file name on Windows
- Check if string ends with certain pattern
- Use Java and RegEx to convert casing in a string
- Java split is eating my characters
- Java String.replace/replaceAll not working
- How to implement a SQL like ‘LIKE’ operator in java?
- Raw Strings in Java – for regex in particular. Multiline strings
- How can I remove punctuation from input text in Java?
- Regular Expressions on Punctuation
- Split string into repeated characters
- Parse Date from String in this format : dd/MM/yyyy [to dd/MM/yyyy]
- Java replace all square brackets in a string