java.util.regex.Matcher.replaceFirst(String replacement)方法用给定的替换字符串替换与模式匹配的第一个子序列。
public String replaceFirst(String replacement)
replacement - 替换字符串。
下面的示例演示java.util.regex.Matcher.replaceFirst(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.replaceFirst(REPLACE); System.out.println(INPUT); } }
让无涯教程编译并运行以上程序,这将产生以下输出-
链接:https://www.learnfk.comhttps://www.learnfk.com/javaregex/javaregex-matcher-replacefirst.html
来源:LearnFk无涯教程网
The cat says meow All dogs say meow.
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)