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