Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

String substitution is a standard operation when we process strings in Java.

Thanks to the handy replaceAll() method in the String class, we can easily do string substitution with regular expressions. However, sometimes the expressions can be confusing, for example, \s and \s+. 

In this short tutorial, we’ll have a look at the difference between the two regular expressions through examples.

2. The Difference Between \s and \s+

The regular expression \s is a predefined character class. It indicates a single whitespace character. Let’s review the set of whitespace characters:

[ \t\n\x0B\f\r]

The plus sign + is a greedy quantifier, which means one or more times. For example, expression X+ matches one or more X characters.

Therefore, the regular expression \s matches a single whitespace character, while \s+ will match one or more whitespace characters.

3. replaceAll() With a Non-Empty Replacement

We’ve learned the meanings of regular expressions \s and \s+.

Now, let’s have a look at how the replaceAll() method behaves differently with these two regular expressions.

We’ll use a string as the input text for all examples:

String INPUT_STR = "Text   With     Whitespaces!   ";

Let’s try passing \s to the replaceAll() method as an argument:

String result = INPUT_STR.replaceAll("\\s", "_");
assertEquals("Text___With_____Whitespaces!___", result);

The replaceAll() method finds single whitespace characters and replaces each match with an underscore. We have eleven whitespace characters in the input text. Thus, eleven replacements will occur.

Next, let’s pass the regular expression \s+ to the replaceAll() method:

String result = INPUT_STR.replaceAll("\\s+", "_");
assertEquals("Text_With_Whitespaces!_", result);

Due to the greedy quantifier +, the replaceAll() method will match the longest sequence of contiguous whitespace characters and replace each match with an underscore.

In our input text, we have three sequences of contiguous whitespace characters. Therefore, each of the three will become an underscore.

4. replaceAll() With an Empty Replacement

Another common usage of the replaceAll() method is to remove matched patterns from the input text. We usually do it by passing an empty string as the replacement to the method.

Let’s see what result we’ll get if we remove whitespace characters using the replaceAll() method with the \s regular expression:

String result1 = INPUT_STR.replaceAll("\\s", "");
assertEquals("TextWithWhitespaces!", result1);

Now, we’ll pass the other regular expression \s+ to the replaceAll() method:

String result2 = INPUT_STR.replaceAll("\\s+", "");
assertEquals("TextWithWhitespaces!", result2);

Because the replacement is an empty string, the two replaceAll() calls produce the same result, even though the two regular expressions have different meanings:

assertEquals(result1, result2);

If we compare the two replaceAll() calls, the one with \s+ is more efficient. This is because it does the job with only three replacements while the call with \s will do eleven replacements.

5. Conclusion

In this short article, we learned about the regular expressions \s and \s+.

We also saw how the replaceAll() method behaved differently with the two expressions.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW