Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Splitting With Math Expression

Tags:

java

math

I am trying to split a Math Expression.

String number = "100+500";

String[] split = new String[3];

I want to make

  • split[0] = "100"
  • split[1] = "+"
  • split[2] = "500"

I tried this but I don't know what to write for splitting.

split = number.split(????);
like image 521
berkc Avatar asked Jan 06 '15 22:01

berkc


People also ask

What is mathematical expression in Java?

An arithmetic expression in Java is a sequence of numeric literals and/or numeric variables separated by arithmetic operators. The result of an arithmetic expression is a number.

How do you split a string using operators?

Method #1 : Using re. split() This task can be solved using the split functionality provided by Python regex library which has power to split the string according to certain conditions and in this case all numbers and operators.


2 Answers

You want to split between digits and non-digits without consuming any input... you need look arounds:

String[] split = number.split("(?<=\\d)(?=\\D)|(?<=\\D)(?=\\d)");

What the heck is that train wreck of a regex?

It's expressing the initial sentence of this answer:

  • (?<=\d) means the previous character is a digit
  • (?=\D) means the next character is a non-digit
  • (?<=\d)(?=\D) together will match between a digit and a non-digit
  • regexA|regexB means either regexA or regexB is matched, which is used as above points, but non-digit then digit for the visa-versa logic

An important point is that look arounds are non-consuming, so the split doesn't gobble up any of the input during the split.


Here's some test code:

String number = "100+500-123/456*789";
String[] split = number.split("(?<=\\d)(?=\\D)|(?<=\\D)(?=\\d)");
System.out.println(Arrays.toString(split));

Output:

[100, +, 500, -, 123, /, 456, *, 789]

To work with numbers that may have a decimal point, use this regex:

"(?<=[\\d.])(?=[^\\d.])|(?<=[^\\d.])(?=[\\d.])"

which effectively just add . to the characters that are a "number".

like image 138
Bohemian Avatar answered Oct 17 '22 04:10

Bohemian


Off the bat, I don't know any library routine for the split. A custom splitting routine could be like this:

/**
 * Splits the given {@link String} at the operators +, -, * and /
 * 
 * @param string
 *            the {@link String} to be split.
 * @throws NullPointerException
 *             when the given {@link String} is null.
 * @return a {@link List} containing the split string and the operators.
 */
public List<String> split(String string) throws NullPointerException {
    if (string == null)
        throw new NullPointerException("the given string is null!");
    List<String> result = new ArrayList<String>();

    // operators to split upon
    String[] operators = new String[] { "+", "-", "*", "/" };

    int index = 0;
    while (index < string.length()) {
        // find the index of the nearest operator
        int minimum = string.length();
        for (String operator : operators) {
            int i = string.indexOf(operator, index);
            if (i > -1)
                minimum = Math.min(minimum, i);
        }

        // if an operator is found, split the string
        if (minimum < string.length()) {
            result.add(string.substring(index, minimum));
            result.add("" + string.charAt(minimum));
            index = minimum + 1;
        } else {
            result.add(string.substring(index));
            break;
        }
    }

    return result;
}

Some test code:

System.out.println(split("100+10*6+3"));
System.out.println(split("100+"));

Output:

[100, +, 10, *, 6, +, 3]
[100, +]
like image 3
Niels Billen Avatar answered Oct 17 '22 04:10

Niels Billen