Ian Limarta Ian Limarta - 3 months ago 7
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

ax+by=c
, the output should be
{a,b,c}
.

Helpful bonus: Is there a way to delete the empty elements that is created when I split. For example, if I type equation
2x+3y=6
, I end up with a "raw" array that contains the elements
{2,,3,,6}


Code:

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++)
{
if(!(termRaw[k].equals("")))
{
constants.add(Integer.parseInt(termRaw[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;
}

Answer

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("")) {
        constants.add(1);
    }
    else if (termRaw[k].equals("-")) {
        constants.add(-1);
    }
    else {
        constants.add(Integer.parseInt(termRaw[k]));
    }
}