I donβt think you can do this with a simple replaceAll (...), you have to write a few lines, for example:
Pattern digitPattern = Pattern.compile("(\\d)"); // EDIT: Increment each digit. Matcher matcher = digitPattern.matcher("test1check2"); StringBuffer result = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(result, String.valueOf(Integer.parseInt(matcher.group(1)) + 1)); } matcher.appendTail(result); return result.toString();
There are probably some syntax errors here, but they will work something like this.
EDIT:. You commented that each digit must be increased separately (abc12d β abc23d) so that the pattern is changed from (\\ d +) to (\\ d)
EDIT 2: Change StringBuilder to StringBuffer as required by the Matcher class.
source share