I am trying to split a Math Expression.
String number = "100+500";
String[] split = new String[3];
I want to make
I tried this but I don't know what to write for splitting.
split = number.split(????);
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.
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.
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-digitregexA|regexB
means either regexA or regexB is matched, which is used as above points, but non-digit then digit for the visa-versa logicAn 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".
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, +]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With