-1

If I had this string "dog dog dog" I want to turn it into something like "cat bird turtle".

If I run code like this:

Pattern p = Pattern.compile("dog");
 Matcher m = p.matcher("dog dog dog");
    while(m.find())
       {
          System.out.println("group:"+m.group());

   }

I get something like this:

dog

dog

dog

But is there a way to essentially overwrite each dog once it's found with a different word to get my desired output mentioned above? i.e. dog1 -> cat, dog2 -> bird, dog3 -> turtle

Community
  • 1
  • 1
user817129
  • 522
  • 3
  • 10
  • 19
  • 1.) Create an array of strings. 2.) Inside the while statement replace dog with the current string and then point at the next string in the array. 3.) start the while loops next iteration – HopAlongPolly Sep 28 '13 at 07:05
  • I think you're looking for regex groups or named groups to use for your replacements. See if any of the following resonate with what you want to do: [SO link1](http://stackoverflow.com/questions/1277157/java-regex-replace-with-capturing-group), [SO link2](http://stackoverflow.com/questions/988655/can-i-replace-groups-in-java-regex) and [SO link3](http://stackoverflow.com/questions/415580/regex-named-groups-in-java) – n0741337 Sep 28 '13 at 07:16
  • Do you think my solution would have anything to do with: m.appendReplacement(...) ? I'm not familiar with that method or understand how it works. – user817129 Sep 28 '13 at 07:32

2 Answers2

0

Yes, you can do that very easily.

  1. Create an array holding the three values, you want to replace
  2. Create an int variable and assign value as zero.
  3. When you find a match of "dog" inside loop, just replace it with latest variable variable state created in step 2. Use that variable as index for the array created in 1 step.
  4. Increment the int variable value by 1.
  5. Reset the matcher with new string.

Note that this code doesn't consider all scenarios and you may need to reset the counter of int variable, if there are more than 3 dogs in the statement. Besides Regex is fun, you will find more information here.

String haystack = "Dog Dog Dog";

Pattern p = Pattern.compile("Dog");

String[] animals = {"cat", "bird", "turtle"};

private void findAndReplaceDog() {

Matcher matcher = p.matcher(haystack); 
int i=0;
while(matcher.find()){
haystack = matcher.replaceFirst(animals[i]);
i++;
matcher.reset(haystack);
}

System.out.println(haystack); 
}
CuriousMind
  • 3,143
  • 3
  • 29
  • 54
0

Lots of good ideas. I came up with this (which works and loops):

public static void main(String[] args) {
   Pattern p = Pattern.compile("dog");
   // get a matcher object
   Matcher m = p.matcher("dog dog dog dog dog dog"); 
   StringBuffer sb = new StringBuffer();
   int i=1;
   while(m.find())
   {
      if(i==1){m.appendReplacement(sb,"cat");i++;}
      else if(i==2){m.appendReplacement(sb,"bird");i++;}
      else if(i==3){m.appendReplacement(sb,"turtle");i=1;}

   }
   m.appendTail(sb);

   System.out.println(sb.toString());
user817129
  • 522
  • 3
  • 10
  • 19