java.time.Matcher.quoteReplacement(String s)方法返回指定字符串的文字替换字符串。

static String quoteReplacement - 声明

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

static String quoteReplacement - 返回值

文字字符串替换。

static String quoteReplacement - 示例

下面的示例显示java.time.Matcher.quoteReplacement(String s)方法的用法。

package com.learnfk;

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

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

   public static void main(String[] args) {
      Pattern pattern=Pattern.compile(REGEX);
      
      //获取匹配器对象
      Matcher matcher=pattern.matcher(INPUT); 
      
      try{
         //下面的行将抛出异常
         INPUT=matcher.replaceAll(REPLACE);
      } catch(Exception e){
         System.out.println("Exception: "+ e.getMessage());
      }
      INPUT=matcher.replaceAll(matcher.quoteReplacement(REPLACE));
      System.out.println(INPUT);
   }
}

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

Exception: Illegal group reference: group index is missing
The cat$says meow All cat$s say meow.

参考链接

https://www.learnfk.com/javaregex/javaregex-matcher-quotereplacement.html