I want to remove trailing white spaces and tabs from my code without removing empty lines.
I tried:
\\s+$
and:
([^\\n]
Regex to find trailing and leading whitespaces:
^[ \t]+|[ \t]+$
To remove trailing white space while ignoring empty lines I use positive look-behind:
(?<=\S)\s+$
The look-behind is the way go to exclude the non-whitespace (\S) from the match.
Try just removing trailing spaces and tabs:
[ \t]+$
In Java:
String str = " hello world ";
// prints "hello world"
System.out.println(str.replaceAll("^(\\s+)|(\\s+)$", ""));