// 对于重复的匹配,建议采用如下方式
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);boolean b = m.matches();// 等效于 boolean b = Pattern.matches(regex, input);
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);if(m.matches()){
String s = m.replaceAll("$1****$3");
System.out.println(s);// 158****56789}
2.4、搜索并获取
String input ="Moderation is for cowards.";
String regex ="\\b\\w*o\\w*\\b";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);while(m.find()){
String s = input.substring(m.start(), m.end());
System.out.println(s);}// Moderation// for// cowards