tags:

views:

227

answers:

3

Java:

String result = "B123".replaceAll("B*","e");
System.out.println(result);

The output is: ee1e2e3e Why?

+19  A: 

'*' 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")

Anders Westrup
99% right...it's a greedy search, so if it can match "B", it will. Otherwise, the output would've been "eee1e2e3e"
That doesn't explain the ':' in the output, though...
luiscubal
The ':' is probably a copy-paste error, the supplied code cannot produce a ':' ...
Anders Westrup
I don't think the : was meant to be construed as part of the output.
Daniel Schaffer
Where are the empty string?like this: " B 1 2 3 "?
JSON
The regexp parser will go through your string character by character. At position 0 it will replace 'B' with 'e', at position 1 it will replace '' with 'e', and so on.
Anders Westrup
Yes, the empty strings are between each character, and at the beginning and end of the string.
Daniel Schaffer
+7  A: 

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.

Daniel Schaffer
Whups, thanks Paul! :D
Daniel Schaffer
On the first attempt the regex consumes the 'B', not an empty string. Then it matches the empty string at each position, starting at the position where the first match left off.
Alan Moore
+1  A: 

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 + !!!!

tkotitan
Yeah, that double match thing is pretty bizarre. It threw me a few times before I learned to watch for it. And a +1 for always using + instead of * if you can.
Alan Moore