集册 Java实例教程 正则表达式替换

正则表达式替换

欢马劈雪     最近更新时间:2020-01-02 10:19:05

445
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
正则表达式替换

import java.util.regex.Matcher;

import java.util.regex.Pattern;


public class Main {
/*
来 自*
 时 代 J a v a 公 众 号
*/

  public static void main(String[] argv) {

    String regex = "test";

    String replace = "t";


    Pattern pattern = Pattern.compile(regex);


    Matcher matcher = pattern.matcher("");

    matcher.reset("s");

    System.out.println(matcher.replaceFirst(replace));

    System.out.println(matcher.replaceAll(replace));
    /* from 
    n o w  j a v a  . c o m*/

  }

}