Seema Sharma
Seema Sharma

Reputation: 170

How to separate recursive string by comma

I have recursive string that goes like this,

var1=[[1,2,3], [1,2,3]], var2=true, var3="hello", var4=(var1=[[1,2,3], [1,2,3]], var2=true, var3="hello")

I want to separate the string by commas and desired result is this,

I have tried this regex, (([a-zA-Z0-9]*)=(.*),?\s?)*, to match the something like this, varx=(), but the complete string was matched.

I also tried to do this by traversing the string but was not able to separate strings like varx="...." because the quotes can contain anythings so there was no way to do this.

    public static int fun2(int start_index, String str, int end_index) {
        Stack<Character> charStack = new Stack<>();
        charStack.add(str.charAt(start_index));
        char opp = ' ';
        if (str.charAt(start_index) == '(') {
            opp = ')';
        } else if (str.charAt(start_index) == '[') {
            opp = ']';
        } else if (str.charAt(start_index) == '[')
            while (end_index < str.length() && !charStack.isEmpty()) {
                if (str.charAt(end_index) == str.charAt(start_index)) {
                    charStack.add(str.charAt(start_index));
                } else if (str.charAt(end_index) == opp) {
                    charStack.pop();
                }
                end_index++;
            }
        if (charStack.isEmpty()) {
            System.out.println("correct");
            System.out.println(str.substring(start_index, end_index));
        }
        return end_index;
        // throw error
    }

    public static void fun(String str) {
        int start = 0;
        int end = -1;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == '=') {
                System.out.println("key = " + str.substring(start, end + 1));
                start = i + 1;
                end = start + 1;
                if (str.charAt(start) == '[' || str.charAt(start) == '(' || str.charAt(i) == '"') {
                    System.out.println("value = ");
                    end = fun2(start, str, end);
                    start = end;
                    i = start;
                }
            } else if (str.charAt(i) == ',' || str.charAt(i) == ' ') {
                start++;
                end++;
            } else {
                end++;
            }
        }
    }

Can anyone suggest any regex or piece of code that will do this for me. Thanks in advance.

Upvotes: 0

Views: 177

Answers (1)

The fourth bird
The fourth bird

Reputation: 163342

To get the matches in the example data, you could match the key part without matching an equals sign.

For the value you can either match from an opening till closing parenthesis, or match until the next key part or end of string.

Note that this pattern does not takes any recursion from the parenthesis or square brackets into account. It depends on matching the parenthesis or using the comma as a separator.

[^\s=,]+=(?:\([^()]*\)|.+?)(?=,\s*[^\s=,]+=|$)

Regex demo

In Java with the doubled backslashes

String regex = "[^\\s=,]+=(?:\\([^()]*\\)|.+?)(?=,\\s*[^\\s=,]+=|$)";

Upvotes: 2

Related Questions