The appendReplacement() method of the Matcher class accepts a StringBuffer object and a String (replacement string) as parameters and, appends the input data to the StringBuffer object, replacing the matched content with the replacement string.
Internally, this method reads each character from the input string and appends the String buffer, whenever a match occurs it appends the replacement string instead of matched content part of the string to the buffer and, proceeds from the next position of the matched substring.
While passing the replacement string to this method if you use “/” or “$” they will not be considered as regular characters and an exception occurs −
Example 1
import java.util.regex.Matcher; import java.util.regex.Pattern; public class QuoteReplacement { public static void main(String[] args) { String str = " <p>This <b>is</b> an <b>example</b> HTML <b>script</b>.</p>"; //Regular expression to match contents of the bold tags String regex = "<b>(\\S+)</b>"; System.out.println("Input string: \n"+str); //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(str); //Creating an empty string buffer StringBuffer sb = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb, "sampledata$" ); //Matcher.quoteReplacement("Bo$ld/Data$")); } matcher.appendTail(sb); System.out.println("Contents of the StringBuffer: \n"+ sb.toString() ); } }
Output
Input string:<p>This <b>is</b> an <b>example</b> HTML <b>script</b>.</p>
Exception in thread "main" java.lang.IllegalArgumentException: Illegal group reference: group index is missing at java.util.regex.Matcher.appendReplacement(Unknown Source) at OCTOBER.matcher.QuoteReplacement.main(QuoteReplacement.java:18)
The quote Replacement method of the Matcher class accepts a string value and returns a literal replacement string. i.e. The characters / and $ are ignored in the given string and the results can be used as a parameter to the appendReplacement() method.
Example 2
import java.util.regex.Matcher; import java.util.regex.Pattern; public class QuoteReplacement { public static void main(String[] args) { String str = "<p>This <b>is</b> an <b>example</b> HTML <b>script</b>.</p>"; //Regular expression to match contents of the bold tags String regex = "<b>(\\S+)</b>"; System.out.println("Input string: \n"+str); //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(str); //Creating an empty string buffer StringBuffer sb = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb, Matcher.quoteReplacement("Bo$ld/Data$")); } matcher.appendTail(sb); System.out.println("Contents of the StringBuffer: \n"+ sb.toString() ); } }
Output
Input string: <p>This <b>is</b> an <b>example</b> HTML <b>script</b>.</p> Contents of the StringBuffer: <p>This Bo$ld/Data$ an Bo$ld/Data$ HTML Bo$ld/Data$.</p>
Example 3
import java.util.regex.Matcher; import java.util.regex.Pattern; public class QuoteReplacementExample { public static void main(String[] args) { String input = "This is sample text"; String regex = "[#]"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(input); //Creating an empty string buffer String str = Matcher.quoteReplacement("sampledata"); System.out.println(str); } }
Output
sampledata