Java
 Computer >> コンピューター >  >> プログラミング >> Java

例を使用したJavaのマッチャーappendReplacement()メソッド


java.util.regex.Matcherクラスは、さまざまな一致操作を実行するエンジンを表します。このクラスのコンストラクターはありません。クラスjava.util.regex.Patternのmatches()メソッドを使用して、このクラスのオブジェクトを作成/取得できます。

この(Matcher)クラスのappendReplacement()メソッドは、StringBufferオブジェクトとString(置換文字列)をパラメーターとして受け入れ、入力データを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: \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() );
   }
}

出力

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 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 \n"+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 Tutorialspoint.

  1. 例を使用したJavaのマッチャーpattern()メソッド

    java.util.regex.Matcher classは、さまざまな一致操作を実行するエンジンを表します。このクラスのコンストラクターはありません。クラスjava.util.regex.Patternのmatches()メソッドを使用して、このクラスのオブジェクトを作成/取得できます。 pattern() この方法(マッチャー )クラスは、現在のマッチャーによって解釈されたパターン(オブジェクト)をフェッチして返します。 例1 import java.util.Scanner; import java.util.regex.Matcher; import java.util.reg

  2. 例を使用したJavaのパターンマッチャー()メソッド

    java.util.regex javaのパッケージは、文字シーケンスの特定のパターンを見つけるためのさまざまなクラスを提供します。 このパッケージのパターンクラスは、正規表現のコンパイル済み表現です。 matcher() このクラスのメソッドは、 CharSequenceのオブジェクトを受け入れます 入力文字列を表すクラスとは、指定された文字列を現在の(パターン)オブジェクトで表される正規表現に一致させるMatcherオブジェクトを返します。 例 import java.util.Scanner; import java.util.regex.Matcher; import java