Ian L
Ian L

Reputation: 246

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;
}

Upvotes: 3

Views: 119

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521379

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>();
// after splitting,
// termRaw contains [-22, -77, '', -88, -10, 33, '-']
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]));
    }
}

Upvotes: 2

Raman Sahasi
Raman Sahasi

Reputation: 31851

If you're using java8, then you can use this one line method:

public static int[] decompose(String s) {
    return Arrays.stream(s.replaceAll("[^0-9]", " ").split("\\s+")).mapToInt(Integer::parseInt).toArray();
}

DEMO:

1. Output

[2, 3, 6]

2. Code

import java.util.*;

public class HelloWorld {
    public static void main(String args[]) {
        String s = "2x+3y=6";
        int[] array = decompose(s);
        System.out.println(Arrays.toString(array));
    }

    public static int[] decompose(String s) {
        return Arrays.stream(s.replaceAll("[^0-9]", " ").split("\\s+")).mapToInt(Integer::parseInt).toArray();
    }
}

Upvotes: 1

Related Questions