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

static String quote - 声明

public static String quote(String s)
  • s   -  要被字符串化的字符串。

static String quote - 返回值

文字字符串替换。

static String quote - 示例

下面的示例显示java.util.regex.Pattern.quote(String s)方法的用法。

package com.learnfk;

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));
      
      //获取匹配器对象
      Matcher matcher = pattern.matcher(INPUT); 
      INPUT = matcher.replaceAll(REPLACE);
      System.out.println(INPUT);
   }
}

让无涯教程编译并运行以上程序,这将产生以下输出-

The cat says meow All cat say meow.

参考链接

https://www.learnfk.com/javaregex/javaregex-pattern-quote.html