Your original regex does not work because you forgot to escape the backslash. This should do what you originally tried to do:
string.replaceAll("\\s{4}", " ")
EDIT : I didn’t understand that you really didn’t do this, forgetting to avoid the backslash, and that Qaru “ate” one of your backslashes, as Alan Moore indicated in one of the comments to this answer (the only difference between "\ s" and " \\s " are backlinks around the text). However, my initial answer will not be very useful to you, so ...
For this to be effective, you need to do something with the output of String # replaceAll , as it returns a new string instead of changing the existing one ( Strings are unchangeable , as William Brendel noted in his comment on your question).
string = string.replaceAll("\\s{4}", " ")
Since this answer did not actually add anything useful, instead of deleting it, I am making it a Community Wiki.
source share