There was a question about regex and trying to answer I found another strange things.
String x = "X";
System.out.println(x.replaceAll("X*", "Y"));
This prints YY. why??
String x = "X";
System.out.println(x.replaceAll("X*?", "Y"));
And this prints YXY
Why reluctant regex doesn't match 'X' character? There is "noting"X"nothing"
but why first doesn't match three symbols and matches two and then one instead of three? and second regex matches only "nothing"
s and not X
?
Let's consider them in turn:
"X".replaceAll("X*", "Y")
There are two matches:
X
is matched, and is replaced with Y
.Y
gets added to the output.End result: YY
.
"X".replaceAll("X*?", "Y")
There are also two matches:
Y
gets added to the output. The character at this position, X
, was not consumed by the match, and is therefore copied into the output verbatim.
Y
gets added to the output.End result: YXY
.
The * is a tricky 'quantifier' since it means '0 or more'. Thus, it also matches '0 times X' (i.e. an empty string).
I would use
"X".replaceAll("X+", "Y")
which has the expected behaviour.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With