Search code examples
javaregexnumberscapturing-group

Regex capturing numbers before and after a symbol


I want to be able to send a math expression/equation into my program and have it give me the resulting answer. I'm using regular expressions, to decode the numbers to operate with. This is my current regex code for addition:

Matcher m = Pattern.compile(".*\\D*(\\d{1,})\\s*\\+\\s*(\\d+).*").matcher(e);
if(!m.matches()) return e;
e = ((int) Addition.add(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2)))) + "";

This works well for the second number in m.group(2) but m.group(1) only returns the last number before the operator symbol, and I've tried a few different ways, this one being my latest with no luck. What am I doing wrong?

EDIT: These are the 3 solving methods, and it has some of the test outputs I was testing with.

public double solve(String e) {
    if (!validExpression(e))
        return 0;
    e = e.replace(" ", "");
    boolean doPar = false;
    if (e.contains("(") || e.contains(")")) {
        doPar = true;
        int par = 0;
        for (char c : e.toCharArray()) {
            if (c == '(')
                par++;
            else if (c == ')')
                par--;
        }
        if (par != 0)
            return 0;
    }
    if (doPar)
        e = solveParentheses(e);
    e = solveSmallExpression(e);
    return Double.parseDouble(e.replace("(", "").replace(")", ""));
}

private static String solveSmallExpression(String e) {
    System.out.println(e);
    while(e.contains("^")){
        final Matcher m = Pattern.compile(".*\\D*(\\d{1,})\\s*\\^\\s*(\\d+).*").matcher(e);
        if(!m.matches()) return e;
        System.out.println(m.group(1) + "\t" + m.group(2));
        e = ((int) Exponentiation.raise(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2)))) + "";
    }
    while(e.contains("/") || e.contains("*")) {
        if(e.contains("/")){
            final Matcher m = Pattern.compile(".*\\D*(\\d{1,})\\s*/\\s*(\\d+).*").matcher(e);
            if(!m.matches()) return e;
            System.out.println(m.group(1) + "\t" + m.group(2));
            e = ((int) Division.divide(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2)))) + "";
        }
        if(e.contains("*")){
            final Matcher m = Pattern.compile(".*\\D*(\\d{1,}+)\\s*\\*\\s*(\\d+).*").matcher(e);
            if(!m.matches()) return e;
            System.out.println(m.group(1) + "\t" + m.group(2));
            e = ((int) Multiplication.multiply(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2)))) + "";
        }
    }
    while(e.contains("-") || e.contains("+")) {
        if(e.contains("-")){
            final Matcher m = Pattern.compile(".*\\D*(\\d{1,})\\s*\\-\\s*(\\d+).*").matcher(e);
            if(!m.matches()) return e;
            System.out.println(m.group(1) + "\t" + m.group(2));
            e = ((int) Subtraction.subtract(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2)))) + "";
        }
        if(e.contains("+")){
            final Matcher m = Pattern.compile(".*\\D*(\\d{1,})\\s*\\+\\s*(\\d+).*").matcher(e);
            if(!m.matches()) return e;
            System.out.println(m.group(1) + "\t" + m.group(2));
            e = ((int) Addition.add(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2)))) + "";
        }
    }
    System.out.println(e);
    System.out.println();
    return e;
}

private static String solveParentheses(String e) {
    while (e.contains("(") && e.contains(")")) {
        int start = -1;
        int end = -1;
        for(int i = 0; i < e.length(); i++) {
            char c = e.charAt(i);
            if(c == '(')
                start = i + 1;
            else if(c == ')')
                end = i;
        }

        String sub = e.substring(start, end);
        if(sub.contains("(") && sub.contains(")"))
            sub = solveParentheses(sub);
        sub = solveSmallExpression(sub);
        e = e.replace(e.subSequence(start - 1, end + 1), sub);
    }
    return e;
}

private static boolean validExpression(String e) {
    for (char c : e.toCharArray()) {
        if (!Character.isDigit(c) && c != '(' && c != ')' && c != '^' && c != '+' && c != '-' && c != '/' && c != '*' && c != ' ')
            return false;
    }
    return true;
}

When I print this:
System.out.println((new ExpressionSolver()).solve("(24 * 100) - (24 / 12)"));
This is the output:

24/12
4   12
0

24*100
4   100
400

400-0
0   0
0

0.0

Solution

  • .* is greedy, and will eat the entire string then roll back until it hits something that matches. Since you can have 0 occurrences of a non-digit (\D*) and need at least one digit and the plus sign, the regex will match once it rolls back far enough to hit that.

    You can fix this by using ".*?" instead, a reluctant operator which grabs as few characters as possible.