You can try something like this:
int degree = 1;
Matcher m = Pattern.compile("(?<=\\^)\\d+").matcher(equation);
while (m.find()) {
int exp = Integer.parseInt(m.group());
if (exp > degree)
degree = exp;
}
We're finding all matches of (?<=\^)\d+
. \d+
matches a string of 1 or more digits, and (?<=\^)
is a positive lookbehind to ensure that these digits are preceded by a caret (but does not include this caret in the match).