English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
appendReplacement()
The methods of the Matcher class accept a StringBuffer object and a String (replacement string) as parameters, and then append 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 it to the String buffer. Whenever a match occurs, it replaces the string instead of appending the matching content part to the buffer, and then continues from the next position of the matched substring.
If the use of “}} /” or “ $” will pass the replacement string to this method, then they will not be treated as regular characters, and an exception will occur-
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>" //The regular expression is used to match the content of bold tags String regex = "<b>(\S+)</b>"; System.out.println("Input string: \n")+str); //Create a pattern object Pattern pattern = Pattern.compile(regex); //Match the compiled pattern in the string Matcher matcher = pattern.matcher(str); //Create 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 Result
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 quoteReplacement method of the Matcher class accepts a string value and returns a literal replacement string. That is, the characters in the given string/and $ are ignored, the result can be used asappendReplacement()The method parameter.
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>" //The regular expression is used to match the content of bold tags String regex = "<b>(\S+)</b>"; System.out.println("Input string: \n")+str); //Create a pattern object Pattern pattern = Pattern.compile(regex); //Match the compiled pattern in the string Matcher matcher = pattern.matcher(str); //Create 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 Result
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>
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 = "[#]"; //Create a pattern object Pattern pattern = Pattern.compile(regex); //Match the compiled pattern in the string Matcher matcher = pattern.matcher(input); //Create an empty string buffer String str = Matcher.quoteReplacement("sampledata"); System.out.println(str); } }
Output Result
sampledata