In Java regex, you use a loop Matcher.find()using Matcher.appendReplacement/Tail, which currently only accepts StringBuffer.
So something like this works ( see also on ideone.com ):
String text = "hahaha that funny; not haha but like huahahuahaha";
Matcher m = Pattern.compile("(hu?a){2,}").matcher(text);
StringBuffer sb = new StringBuffer();
int index = 0;
while (m.find()) {
m.appendReplacement(sb,
String.format("%S[%d]", m.group(), ++index)
);
}
m.appendTail(sb);
System.out.println(sb.toString());
API Links
see also