Home >Java >javaTutorial >Why does String.replaceAll(\'.*\', \'a\') result in \'aa\' in Java?
String.replaceAll(regex) Match Behavior
The curious observation that String.replaceAll(".*", "a") results in "aa" raises questions about the nature of the .* regex.
Matching Anything
.* matches any sequence of characters, even an empty string. Therefore, the first match encompasses the entire input string, prompting the regex engine to search for subsequent matches starting from the end.
However, .* can also match an empty string at the end of the input. Hence, it finds a second match and replaces it with an "a," leading to the "aa" outcome.
Using . and .replaceFirst()
To prevent this behavior, use . instead, as it requires at least one character to match. Alternatively, employ .replaceFirst() to limit replacement to the first occurrence.
Behavioral Explanation
The fact that .* matches an empty string is peculiar and deserves deeper exploration. Unlike most regex engines, Java's regex engine shifts one character further in the input after a second match with .*. This deviation is evident in the following illustration:
<code class="text"># Before first run regex: |.* input: |whatever # After first run regex: .*| input: whatever| # Before second run regex: |.* input: whatever| # After second run: since .* can match an empty string, it is satisfied... regex: .*| input: whatever| # However, this means the regex engine matched an empty input. # All regex engines, in this situation, will shift # one character further in the input. # So, before third run, the situation is: regex: |.* input: whatever<|ExhaustionOfInput> # Nothing can ever match here: out</code>
However, it's worth noting that other regex engines like GNU sed consider the input exhausted after the first match.
The above is the detailed content of Why does String.replaceAll('.*', 'a') result in 'aa' in Java?. For more information, please follow other related articles on the PHP Chinese website!