java.util.regex.Pattern.quote() 方法



說明

java.util.regex.Pattern.quote(String s) 方法返回指定 String 的一個文字模式 String。

宣告

以下是 java.util.regex.Pattern.quote(String s) 方法的宣告。

public static String quote(String s)

引數

  • s − 要作為文字表示的字串。

返回

一個文字串替換。

示例

以下示例展示了 java.util.regex.Pattern.quote(String s) 方法的用法。

package com.tutorialspoint;

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.
javaregex_pattern.htm
廣告
© . All rights reserved.