Matcher: appendReplacement(StringBuffer sb,String replacement) : Matcher « java.util.regex « Java by API






Matcher: appendReplacement(StringBuffer sb,String replacement)

/*
 * Output: 
gog gay gaughter gaut gig go gone gate 
 */
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainClass {
  public static void main(String args[]) {
    String joke = "dog day daughter daut did do done date";
    String regEx = "d";

    Pattern doggone = Pattern.compile(regEx);
    Matcher m = doggone.matcher(joke);

    StringBuffer newJoke = new StringBuffer();
    while (m.find())
      m.appendReplacement(newJoke, "g");
    m.appendTail(newJoke);
    System.out.println(newJoke.toString());
  }
}

           
       








Related examples in the same category

1.Matcher.appendTail(StringBuffer sb)
2.Matcher: find()
3.Matcher: group()
4.Matcher: group(int group)
5.Matcher: groupCount()
6.Matcher: lookingAt()
7.Matcher: matches()
8.Matcher: replaceAll(String text)
9.Matcher: start()