提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
匹配后将所有替换为正则表达式中的通配符
import java.util.regex.Pattern; import java.util.regex.Matcher; //来自 nowjava - 时代Java public class ReplaceDemo2 { private static String REGEX = "a*b"; private static String INPUT = "aabfooaabfooabfoob"; private static String REPLACE = "-"; public static void main(String[] args) { Pattern p = Pattern.compile(REGEX); // get a matcher object Matcher m = p.matcher(INPUT); INPUT = m.replaceAll(REPLACE); System.out.println(INPUT); } }