Matcher 类的 quoteReplacement() 方法返回指定字符串的文字替换字符串。该方法可以和matcher对象一起使用,在matcher类的appendReplacement方法中进行替换操作。
用法
public static String quoteReplacement(String s)
参数
s- 要文字化的字符串
返回
文字字符串替换
自从
1.5
例子1
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatcherquoteReplacementExample1{
private static String INPUT ;
public static void main(String[] args) {
//Pattern creation
Pattern pattern = Pattern.compile("Independent");
// creating matcher object
Matcher matcher = pattern.matcher("java is platform Independent ");
INPUT = matcher.replaceAll(matcher.quoteReplacement(":)"));
System.out.println(INPUT);
}
}
输出:
java is platform:)
例子2
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatcherquoteReplacementExample2 {
private static String input ;
public static void main(String[] args) {
//Pattern creation
Pattern p= Pattern.compile("javaprogramming");
//creating matcher object
Matcher m = p.matcher("javaprogramming is secure " + ".javaprogramming is platform");
try{
//Below line will throw exception
input = m.replaceAll("java$");
} catch(Exception e){
System.out.println("Exception:"+ e.getMessage());
}
input = m.replaceAll(m.quoteReplacement("java$"));
System.out.println(input);
}
}
输出:
Exception:Illegal group reference:group index is missing java$ is secure .java$ is platform
相关用法
- Java Matcher quoteReplacement(String)用法及代码示例
- Java Matcher region()用法及代码示例
- Java Matcher matches()用法及代码示例
- Java Matcher replaceAll(String)用法及代码示例
- Java Matcher group(String)用法及代码示例
- Java Matcher start(int)用法及代码示例
- Java Matcher replaceFirst(String)用法及代码示例
- Java Matcher useTransparentBounds()用法及代码示例
- Java Matcher lookingAt()用法及代码示例
- Java Matcher end()用法及代码示例
- Java Matcher useAnchoringBounds()用法及代码示例
- Java Matcher appendTail(StringBuffer)用法及代码示例
- Java Matcher hasAnchoringBounds()用法及代码示例
- Java Matcher toMatchResult()用法及代码示例
- Java Matcher replaceAll(Function)用法及代码示例
- Java Matcher requireEnd()用法及代码示例
- Java Matcher reset(CharSequence)用法及代码示例
- Java Matcher appendReplacement(StringBuilder, String)用法及代码示例
- Java Matcher appendReplacement(StringBuffer, String)用法及代码示例
- Java Matcher region(int, int)用法及代码示例
注:本文由纯净天空筛选整理自 Java Matcher quoteReplacement() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。