Matcher appendReplacement() method in Java with Examples

JavaObject Oriented ProgrammingProgramming

The java.util.regex.Matcher class represents an engine that performs various match operations. There is no constructor for this class, you can create/obtain an object of this class using the matches() method of the class java.util.regex.Pattern.

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 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.

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>HTML <b>script</b>.</p>";
      //Regular expression to match contents of the bold tags
      String regex = "<b>(\S+)</b>";
      System.out.println("Input string: 
"+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:
"+ sb.toString() );    } }

Output

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>

Example 2

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 form 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 
"+buffer.toString());    } }

Output

Enter input text:
Hello# how$ are& yo|u welco<me to> Tut-oria@ls@po-in#t.
Removing the special character form the given string
The are special characters occurred 11 times in the given text
Text after removing all of them
Hello how are you welcome to Tutorialspoint.
raja
Updated on 20-Nov-2019 07:01:17

Advertisements