My Java source code:
String result = "B123".replaceAll("B*","e");
System.out.println(result);
The output is:ee1e2e3e
.
Why?
'*' means zero or more matches of the previous character. So each empty string will be replaced with an "e".
You probably want to use '+' instead:
replaceAll("B+", "e")
You want this for your pattern:
B+
And your code would be:
String result = "B123".replaceAll("B+","e");
System.out.println(result);
The "*" matches "zero or more" - and "zero" includes the nothing that's before the B, as well as between all the other characters.
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