Use this
str = str.replaceAll("(^.$|\\s.\\s|^.\\s|\\s.$)", "").replaceAll("\\s+", " ").trim();
The problem with your solution was that you used \b , which was expecting a character at the end and beginning of a word so that it does not work in your case.
/b
Matches in position between the word character (everything that matches the \ w character) and the non-word character (everything that matches [^ \ w] or \ W), as well as at the beginning and / or end of the line if the first and / or last characters in a string are word characters.
REFER FOR REGULAR EXPRESSION
source share