Regex expressions in Java, \\s vs. \\s+
Asked Answered
T

5

106

What's the difference between the following two expressions?

x = x.replaceAll("\\s", "");
x = x.replaceAll("\\s+", "");
Tosh answered 25/3, 2013 at 21:59 Comment(0)
C
98

The first one matches a single whitespace, whereas the second one matches one or many whitespaces. They're the so-called regular expression quantifiers, and they perform matches like this (taken from the documentation):

Greedy quantifiers
X?  X, once or not at all
X*  X, zero or more times
X+  X, one or more times
X{n}    X, exactly n times
X{n,}   X, at least n times
X{n,m}  X, at least n but not more than m times

Reluctant quantifiers
X?? X, once or not at all
X*? X, zero or more times
X+? X, one or more times
X{n}?   X, exactly n times
X{n,}?  X, at least n times
X{n,m}? X, at least n but not more than m times

Possessive quantifiers
X?+ X, once or not at all
X*+ X, zero or more times
X++ X, one or more times
X{n}+   X, exactly n times
X{n,}+  X, at least n times
X{n,m}+ X, at least n but not more than m times
Chaparajos answered 25/3, 2013 at 22:2 Comment(1)
I've always loved how they provide separate descriptions of the greedy, reluctant, and possessive versions of each quantifier, and then say exactly the same thing about all three. ;)Backstay
P
63

Those two replaceAll calls will always produce the same result, regardless of what x is. However, it is important to note that the two regular expressions are not the same:

  • \\s - matches single whitespace character
  • \\s+ - matches sequence of one or more whitespace characters.

In this case, it makes no difference, since you are replacing everything with an empty string (although it would be better to use \\s+ from an efficiency point of view). If you were replacing with a non-empty string, the two would behave differently.

Phyla answered 25/3, 2013 at 22:4 Comment(2)
W.r.t your first line, If x is "Book Your Domain And Get\n \n\n \n \n \n Online Today." Will both produce same results?Redwing
@user3705478 Both will produce the same results, even if there would be multiple spaces after each other. The difference lies in the way it get's handled. If you would have a group of (for example) 3 spaces directly following each other \\s+ takes that group and turns the whole it into a "", while \\s would proces every space on its own.Bonn
P
10

First of all you need to understand that final output of both the statements will be same i.e. to remove all the spaces from given string.

However x.replaceAll("\\s+", ""); will be more efficient way of trimming spaces (if string can have multiple contiguous spaces) because of potentially less no of replacements due the to fact that regex \\s+ matches 1 or more spaces at once and replaces them with empty string.

So even though you get the same output from both it is better to use:

x.replaceAll("\\s+", "");
Percy answered 25/3, 2013 at 22:4 Comment(0)
S
2

The first regex will match one whitespace character. The second regex will reluctantly match one or more whitespace characters. For most purposes, these two regexes are very similar, except in the second case, the regex can match more of the string, if it prevents the regex match from failing.  from http://www.coderanch.com/t/570917/java/java/regex-difference

Struck answered 25/3, 2013 at 22:2 Comment(1)
Scratch the word "reluctantly". This question is about \s+, not \s+? like that other question.Backstay
F
0

Because your second parameter is empty string "", there is no difference between the output of two cases. But if your second parameter was a anything other than blank e.g. "_" then the output would have been different. For example if:

String x = "Text   With     Whitespaces!   ";

then

x.replaceAll("\\s", "_"); 

will give you

"Text___With_____Whitespaces!___"

but

x.replaceAll("\\s+", "_");

will give you

"Text_With_Whitespaces!_"

see this for more info: https://www.baeldung.com/java-regex-s-splus

Forest answered 17/11, 2022 at 6:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.