Javaregex-pattern-quote

提供:Dev Guides
移動先:案内検索

java.util.regex.Pattern.quote()メソッド

説明

  • java.util.regex.Pattern.quote(String s)*メソッドは、指定されたストリングのリテラルパターンストリングを返します。

宣言

次に、* java.util.regex.Pattern.quote(String s)*メソッドの宣言を示します。

public static String quote(String s)

パラメーター

  • s -リテラル化される文字列。

返品

リテラル文字列の置換。

次の例は、java.util.regex.Pattern.quote(String s)メソッドの使用方法を示しています。

package com.finddevguides;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternDemo {
   private static String REGEX = "dog$";
   private static String INPUT = "The dog$ says meow " + "All dog$ say meow.";
   private static String REPLACE = "cat";

   public static void main(String[] args) {
      Pattern pattern = Pattern.compile(Pattern.quote(REGEX));

     //get a matcher object
      Matcher matcher = pattern.matcher(INPUT);
      INPUT = matcher.replaceAll(REPLACE);
      System.out.println(INPUT);
   }
}

上記のプログラムをコンパイルして実行すると、次の結果が生成されます-

The cat says meow All cat say meow.