I assumed that \n not the actual line feed, but also works with linefeeds . This should work fine:
test=test.replaceAll ("(?:\\s|\\\n)+"," ");
If there is no textual \n , this could be simpler:
test=test.replaceAll ("\\s+"," ");
And you need to trim leading / trailing spaces.
I use the RegexBuddy tool to test any single regular expression that is very convenient for many languages.
source share