Search code examples
javaregexstringreplaceall

Why does the 'replaceAll' method not add an empty space at the beginning of the String?


I have a string with multiple white spaces in the beginning, middle and end: " Humpty Dumpty sat ".

I used regular expression (https://stackoverflow.com/a/2932439/13136767) to remove the extra whitespaces and replace it with group 1 (which is an empty space).

String str = "        Humpty   Dumpty   sat  ";
str = str.replaceAll("^ +| +$|( )+", "$1");
System.out.println("[" + str + "]");

Expected Output:

[ Humpty Dumpty sat ]

Actual Output:

[Humpty Dumpty sat]

A replacement string, is the text that each regular expression match is replaced with during a search-and-replace. The large whitespace at the beginning of the String should have been replaced by an empty space. Why did it not add an empty space, here, at the beginning of the String?


Solution

  • Why did it not add an empty space, here, at the beginning of the String?

    Because the regex you're using is specifically designed not to add spaces at the beginning or end of the string:

    str.replaceAll("^ +| +$|( )+", "$1");
    

    Here we have three alternatives: ^ +, +$ and ( )+. All three alternatives match one or more spaces. The difference is that the first two only match at the beginning and end of the string respectively and that only the third one contains a capturing group. So if the third one is matched, i.e. if the sequence of spaces is not at the beginning or end of the string, the value of $1 will be a space. Otherwise it will be empty.

    The whole point of this is to not add spaces at the beginning or end. If you don't want this behaviour, you don't need any of this complexity. Just replace one or more spaces with a single space and that's it.