import java.util.regex.Matcher;
import java.util.regex.Pattern;
@SuppressWarnings("all")
public class Run {
public static void main(String[] args) {
//pattern类不定义任何构造函数,因此Pattern类通过静态方法compile()获取
//指定的字符串为john
Pattern pattern = Pattern.compile("john");
//匹配器类,通过解释 Pattern 对 character sequence 执行匹配操作的引擎。
Matcher m = pattern.matcher("john does this,and john does that");
//用sam来代替所有的john
String str = m.replaceAll("sam");
System.out.println(str);
}
}
替换指定字符串中的全部指定字符
最新推荐文章于 2024-07-02 17:37:04 发布