7

Given this code :

String replaced = "A".replaceAll(".*", "HI");

Why does replaced contain the string HIHI instead of HI as I would have guessed? It seems that it has something to do with the beginning of a line since using the pattern ^.* yields HI, but I don't get the reason for this.

midhunhk
  • 5,560
  • 7
  • 52
  • 83
maerch
  • 2,055
  • 3
  • 17
  • 24

4 Answers4

5

Look at the replaceAll javadoc: Replaces each substring of this string that matches the given regular expression with the given replacement. This matches two substrings: "" and "A". You can see this by testing

String replaced = "".replaceAll( ".*", "HI" );

Which results in a single "HI" being being printed

Oded Peer
  • 2,377
  • 18
  • 25
  • 2
    In "A" there are two positions (between the letters so to say): 0 and 1. At 0 "A" matches, at 1 "" matches. – Joop Eggen Apr 22 '13 at 10:18
  • @JoopEggen Anyway it's counter-intuitive. If `.*` is greedy it should "eat" entire string **including** the empty string at the end and return a match once. – Adam Dyga Apr 22 '13 at 10:29
  • two empty strings `"A".replaceAll("", "HI")==HIAHI` – Kent Apr 22 '13 at 10:30
  • @AdamDyga "greedy" (matching longest possible string) at position 0, but at position 1 it starts matching again. – Joop Eggen Apr 22 '13 at 11:05
5

I think this is because .* first matches the entire string, and then matches the empty string at the end of string. Of course, ^.* won't match the empty string at the end of "A", so you end up with only one "HI".

Étienne Miret
  • 6,448
  • 5
  • 24
  • 36
0

The find method of the Matcher class finds "A" and an empty String after the "A", so there are 2 replacements.

Michael Besteck
  • 2,415
  • 18
  • 10
0

The replaceAll method takes regex and replacement parameter as like (read more) :-

public String replaceAll(String regex,
                         String replacement)

In this example .* represents the regular expression.

. indicates Any character (may or may not match line terminators)

* indicates zero or more times (Read More regexp)

The output of your given code is right . The regex is matches with * means zero or more times . And it affects the result.

String replaced = "A".replaceAll(".*", "HI");

Output :- HIHI

Hope it will help you.

JDGuide
  • 6,239
  • 12
  • 46
  • 64