Ian Limarta Ian Limarta - 1 month ago 5x
Java Question

How would I split this expression with regex?

I'm working on solving an equation but I will like to use the constants to program my solution.

The method I'm working on is called decompose which decomposes the equation into constants. The problem is that when I split, an equation with a negative constant will yield an array with the absolute value of the constants. How can I achieve the minus sign while still using regex?

If the input is

, the output should be

Helpful bonus: Is there a way to delete the empty elements that is created when I split. For example, if I type equation
, I end up with a "raw" array that contains the elements


public static int[] decompose(String s)
s = s.replaceAll(" ", "");

String[] termRaw = s.split("\\D"); //Splits the equation into constants *and* empty spaces.
ArrayList<Integer> constants = new ArrayList<Integer>(); //Values are placed into here if they are integers.
for(int k = 0 ; k < termRaw.length ; k++)

int[] ans = new int[constants.size()];

for(int k = 0 ; k < constants.size(); k++) //ArrayList to int[]
ans[k] = constants.get(k);
return ans;


The general strategy to this answer is to split the input equation by operator, then extract out the coefficients in a loop. However, there are several edge cases which need to be considered:

  • a plus symbol (+) is prefixed to every minus which does not appear either as the first term
  • after splitting, a coefficient of positive one is detected by seeing empty string
  • after splitting, a coefficient of negative one is detected by seeing a minus sign

String input = "-22x-77y+z=-88-10+33z-q";
input = input.replaceAll(" ", "")             // remove whitespace
             .replaceAll("=-", "-");          // remove equals sign
             .replaceAll("(?<!^)-", "+-");    // replace - with +-, except at start of line
// input = -22x+-77y+z+-88+-10+33z+-

String[] termRaw = bozo.split("[\\+*/=]");
// termRaw contains [-22x, -77y, z, -88, -10, 33z, -]

ArrayList<Integer> constants = new ArrayList<Integer>();
for (int k=0 ; k < termRaw.length ; k++) {
    termRaw[k] = termRaw[k].replaceAll("[a-zA-Z]", "");
    if (termRaw[k].equals("")) {
    else if (termRaw[k].equals("-")) {
    else {