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

Método appendReplacement() de Matcher com exemplo em Java

The java.util.regex.Matcher class represents the engine for executing various matching operations. This class has no constructor, and can be created using the matchs() method of the class java.util.regex.Pattern/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.

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> um exemplo <b>exemplo</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);
      //Criando um objeto padrão
      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("Conteúdo do StringBuffer: \n");+ sb.toString());
   }
}

Resultados de saída

Stringa de entrada:
<p>Este é <b>uma</b>/b> um exemplo <b>exemplo</b>/b> HTML <b>script</b>/b>.</p>
Conteúdo do StringBuffer:
Este dado em negrito é um dado em negrito HTML.
<p>Este dado em negrito é um dado em negrito HTML.</p>

Exemplo2

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("Digite o texto de entrada: ");
      String input = sc.nextLine();
      String regex = "[#$&+@=|<>-];";
      //Criando um objeto padrão
      Pattern pattern = Pattern.compile(regex);
      //Criando um objeto Matcher
      Matcher matcher = pattern.matcher(input);
      int count = 0;
      StringBuffer buffer = new StringBuffer();
      System.out.println("Removendo o caractere especial da string fornecida");
      while(matcher.find()) {
         count++;
         matcher.appendReplacement(buffer, "");
      }
      matcher.appendTail(buffer);
      //Recuperando o padrão usado
      System.out.println("Foram encontrados caracteres especiais "+count+"vezes no texto fornecido");
      System.out.println("Texto após remover todos eles \n"+buffer.toString());
   }
}

Resultados de saída

Insira o texto de entrada:
Olá# como$ estão& você| bem<venido> ao> Tut-oria@ls@po-in#t.
Removendo o caractere especial da string fornecida
Foram encontrados caracteres especiais 11 vezes no texto fornecido
Texto após remover todos eles
Olá, como vai você, bem-vindo ao w3codebox.