java.util.regex.Matcher.replaceAll(String replacement)方法使用给定的替换字符串替换与该模式匹配的每个子序列。
String replaceAll - 声明
public String replaceAll(String replacement)
replacement - 替换字符串。
String replaceAll - 返回值
通过用替换字符串替换每个匹配的子序列并根据需要替换捕获的子序列来构造该字符串。
String replaceAll - 示例
下面的示例显示java.util.regex.Matcher.replaceAll(String replacement)方法的用法。
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); INPUT = matcher.replaceAll(REPLACE); System.out.println(INPUT); } }
让无涯教程编译并运行以上程序,这将产生以下输出-
The cat says meow All cats say meow.
参考链接
https://www.learnfk.com/javaregex/javaregex-matcher-replaceall.html
标签:regex,Java,String,无涯,replaceAll,static,INPUT,replacement From: https://blog.51cto.com/u_14033984/9047799