English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The java.util.regex.Matcher class represents an engine for executing various matching operations. This class has no constructor and can be created using the matches() method of the java.util.regex.Pattern class/Get an object of this class.
The appendReplacement() method of this (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 it to the String buffer, and 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.
import java.util.regex.Matcher; import java.util.regex.Pattern; public class appendReplacementExample { 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, "BoldData"); } matcher.appendTail(sb);} System.out.println("Contents of the StringBuffer:\n");+ sb.toString()); } }
Resultado de salida
Input string: <p>This <b>is</b> an <b>example</b> HTML <b>script</b>.</p> Contents of the StringBuffer: This BoldData an BoldData HTML BoldData. <p>This BoldData an BoldData HTML BoldData.</p>
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class appendReplacementExample { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter input text:"); String input = sc.nextLine(); String regex = "[#$&+@=|<>-]"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Creating a Matcher object Matcher matcher = pattern.matcher(input); int count = 0; StringBuffer buffer = new StringBuffer(); System.out.println("Removing the special character from the given string"); while(matcher.find()) { count++; matcher.appendReplacement(buffer, ""); } matcher.appendTail(buffer); //Recuperando el patrón utilizado System.out.println("Se produjeron caracteres especiales "+count+"veces en el texto proporcionado"); System.out.println("Texto después de eliminar todos ellos \n"+buffer.toString()); } }
Resultado de salida
Introduce el texto de entrada: Hola# cómo$ estás& bien|venido< a> Tut-oria@ls@po-en#t. Eliminando el carácter especial de la cadena proporcionada Se produjeron caracteres especiales 11 veces en el texto proporcionado Texto después de eliminar todos ellos Hola, ¿cómo estás? Bienvenido a w3codebox.