Matcher.appendTail(StringBuffer sb)

StringBuffer appendTail(StringBuffer sb)
Implements a terminal append-and-replace step.

/*
 goat goats
 */
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainClass {

  public static void main(String[] av) {
    String joke = "dog " + "dogs";
    String regEx = "dog";

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

    StringBuffer newJoke = new StringBuffer();
    while (m.find()) {
      m.appendReplacement(newJoke, "goat");
    }
    m.appendTail(newJoke);
    System.out.println(newJoke.toString());
  }
}
Home 
  Java Book 
    Essential Classes  

Matcher:
  1. Regular Expression Processing
  2. Normal character
  3. Wildcard character
  4. Using Wildcards and Quantifiers
  5. Greedy behavior
  6. Working with Classes of Characters
  7. Using replaceAll( )
  8. Using split( )
  9. Matcher: appendReplacement(StringBuffer sb,String replacement)
  10. Matcher.appendTail(StringBuffer sb)
  11. Matcher: find()
  12. Matcher: group()
  13. Matcher: group(int group)
  14. Matcher: groupCount()
  15. Matcher: lookingAt()
  16. Matcher: matches()
  17. Matcher: replaceAll(String text)
  18. Matcher: start()