public static String replaceAll(String str, String pattern, String replacePattern) {
if (str == null || pattern == null || replacePattern == null) {
return str;
}
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(str); StringBuffer b = new StringBuffer();
while (m.find()) {
String tmp = new String(replacePattern);
for (int i = 1; i <= m.groupCount(); i++) {
tmp = tmp.replaceAll("\\{" + i + "\\}", m.group(i));
}
m.appendReplacement(b, tmp);
}
m.appendTail(b);
return b.toString();
}
public static void main(String args[]) throws Exception {
String str = "a3ic209a294ub43tb";
str = replaceAll(str,"([^abc]*)([abc])([^abc]*)","{1}x{2}y{3}");
System.out.println(str);
}
if (str == null || pattern == null || replacePattern == null) {
return str;
}
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(str); StringBuffer b = new StringBuffer();
while (m.find()) {
String tmp = new String(replacePattern);
for (int i = 1; i <= m.groupCount(); i++) {
tmp = tmp.replaceAll("\\{" + i + "\\}", m.group(i));
}
m.appendReplacement(b, tmp);
}
m.appendTail(b);
return b.toString();
}
public static void main(String args[]) throws Exception {
String str = "a3ic209a294ub43tb";
str = replaceAll(str,"([^abc]*)([abc])([^abc]*)","{1}x{2}y{3}");
System.out.println(str);
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货