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.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.

