Tarikh Chouhan February 2016

Regex does not store the element in the first index

I have a function which takes a String containing a math expression such as 6+9*8 or 4+9 and it evaluates them from left to right (without normal order of operation rules).

I've been stuck with this problem for the past couple of hours and have finally found the culprit BUT I have no idea why it is doing what it does. When I split the string through regex (.split("\\d") and .split("\\D")), I make it go into 2 arrays, one is a int[] where it contains the numbers involved in the expression and a String[] where it contains the operations.

What I've realized is that when I do the following:

String question = "5+9*8";
String[] mathOperations = question.split("\\d");
for(int i = 0; i < mathOperations.length; i++) {
    System.out.println("Math Operation at " + i + " is " + mathOperations[i]);
}

it does not put the first operation sign in index 0, rather it puts it in index 1. Why is this? This is the system.out on the console:

Math Operation at 0 is 
Math Operation at 1 is +
Math Operation at 2 is *

Answers


radoh February 2016

Because on position 0 of mathOperations there's an empty String. In other words

mathOperations = {"", "+", "*"};

According to split documentation

The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. ...

Why isn't there an empty string at the end of the array too?

Trailing empty strings are therefore not included in the resulting array.

More detailed explanation - your regex matched the String like this:

"(5)+(9)*(8)" -> "" + (5) + "+" + (9) + "*" + (8) + ""

but the trailing empty string is discarded as specified by the documentation. (hope this silly illustration helps)

Also a thing worth noting, the regex you used "\\d", would split following string "55+5" into

["", "", "+"]

That's because you match only a single character, you should probably use "\\d+"


J. V. A. February 2016

You may find the following variation on your program helpful, as one split does the jobs of both of yours...

public class zw {
    public static void main(String[] args) {
            String question = "85+9*8-900+77";
            String[] bits = question.split("\\b");
            for (int i = 0; i < bits.length; ++i) System.out.println("[" + bits[i] + "]");
    }
}

and its output:

[]
[85]
[+]
[9]
[*]
[8]
[-]
[900]
[+]
[77]

In this program, I used \b as a "zero-width boundary" to do the splitting. No characters were harmed during the split, they all went into the array.

More info here: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html and here: http://www.regular-expressions.info/wordboundaries.html

Post Status

Asked in February 2016
Viewed 3,772 times
Voted 10
Answered 2 times

Search




Leave an answer