String[] parts = input.split("(?<=\\D)(?=\\d+$)"); if (parts.length < 2) throw new IllegalArgumentException("Input does not end with numbers: " + input); String head = parts[0]; String numericTail = parts[1];
This more elegant solution uses the look and look of regular expression features.
Explanation:
(?<=\\D) means at the current point, make sure that the previous characters do not end with a digit (no digit is expressed as \D )(?=\\d+$) means t current point, make sure that only digits are found at the end of the input (the digit is expressed as \D )
It will only be truth at the right point, which you want to divide by input
source share