English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Matcher appendReplacement() method with example in Java

The java.util.regex.Matcher class represents an engine for performing 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. 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.

Example1

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>/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());
   }
}

Output Result

Input string:
<p>This <b>is</b>/b> an <b>example</b>/b> HTML <b>script</b>/b>.</p>
Contents of the StringBuffer:
This BoldData an BoldData HTML BoldData.
<p>This BoldData an BoldData HTML BoldData.</p>

Example2

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);
      //Retrieving Pattern used
      System.out.println("The are special characters occurred ");+count+"times in the given text");
      System.out.println("Text after removing all of them \n");+buffer.toString());
   }
}

Output Result

Enter input text:
Hello# how$ are& you| welco<me to> Tut-oria@ls@po-in#t.
Removing the special character from the given string
Special characters occurred 11 times in the given text
Text after removing all of them
Hello, how are you? Welcome to the3codebox.