str.replaceAll("^([0-9]+)", "<sup>$1</sup>");
String input = "abcd"; Pattern p = Pattern.compile("regexp"); String output = p.matcher(input).replaceAll("replacement");