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

استخدام طريقة Matcher appendReplacement() مع مثال في Java

تمثل فئة java.util.regex.Matcher محرك تنفيذ عمليات التطابق المختلفة. لا تحتوي هذه الفئة على بناء، يمكن إنشاء/الحصول على هذا الفئة باستخدام طريقة matchs() من الفئة java.util.regex.Pattern.

يقبل طريقة appendReplacement() من الفئة Matcher StringBuffer وString (النص المستبدل) كمعلمات، ويضيف البيانات المدخلة إلى StringBuffer، ويستبدل النص المتطابق بالنص المستبدل.

في الداخل، يقرأ هذا الطريقة كل حرف من النص المدخل ويضعه في منطقية StringBuffer، وعند كل تطابق، يتم استبدال النص بدلاً من إضافة جزء التطابق من النص إلى منطقية StringBuffer، ثم يستمر من الموقع التالي للنص المتطابق.

النموذج 1

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

نتیجه خروجی

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>

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

نتیجه خروجی

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 w3codebox.
أنت قد تحب