Java:
String result = "B123".replaceAll("B*","e");
System.out.println(result);
The output is: ee1e2e3e Why?
Java:
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.
I spent over a month working at a big tech company fixing a bug with * (splat!) in regular expressions. We maintained a little-known UNIX OS. My head nearly exploded because it matches ZERO occurrences of an encounter with a character. Talk about a hard bug to understand through your own recreates. We were double substituting in some cases. I couldn't figure out why the code was wrong, but was able to add code that caught the special (wrong) case and prevented double subbing and didn't break any of the utilities that included it (including sed and awk). I was proud to have fixed this bug, but as already mentioned.
For god's sake, just use + !!!!