Вот решение, которое игнорирует дополнительные сложности с x ^ 1 и x ^ 0 и коэффициентом = 1.
. Использует Lookahead в регулярном выражении, как описано здесь
import java.util.ArrayList;
import java.util.List;
public class MyClass {
public static void main(String[] args) {
// expect format ax^n for each term. in particular in the cases a=1, x=1 and x=0.
String poly = "26x^7+5x^6-8x^3+1x^1-2x^0";
// remove ^ and then split by x and by + and - keeping the sign
String[] numbers = poly.replace("^", "").split("((?=\\+)|(?=\\-)|x)");
List<Integer> coeff = new ArrayList<>();
List<Integer> expo = new ArrayList<>();
// we can now assume that for every coefficient there is an exponent
for (int i = 0; i < numbers.length; i += 2) {
coeff.add(Integer.parseInt(numbers[i]));
expo.add(Integer.parseInt(numbers[i + 1]));
}
System.out.println(coeff);
System.out.println(expo);
}
}
Выход:
[26, 5, -8, 1, -2]
[7, 6, 3, 1, 0]