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);
  }