I am splitting equation string into string array like this:
String[] equation_array = (equation.split("(?<=[-+×÷)(])|(?=[-+×÷)(])"));
Now for test string:
test = "4+(2×5)"
result is fine:
test_array = {"4", "+", "(", "2",...}
but for test string:
test2 = "(2×5)+5"
I got string array:
test2_array = {"", "(", "×",...}.
So, problem is why does it add an empty string before (
in array after splitting?
This is actually known behavior in Java regex.
To avoid this empty result use this negative lookahead based regex
:
String[] equation_array = "(2×5)+5".split("(?!^)((?<=[-+×÷)(])|(?=[-+×÷)(]))");
//=> ["(", "2", "×", "5", ")", "+", "5"]
What (?!^)
means is to avoid splitting at line start.