0

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,

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

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.

0

1 Answer 1

2

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=,]+=|$)";
Sign up to request clarification or add additional context in comments.

2 Comments

not working with this string v1=[[1,2,3], [2,3,4]], v3=(v1=[[1,2,3], [2,3,4]], v3=(), v4="123"), v4="123" the output here is v1=[[1,2,3], [2,3,4]] v3=(v1=[[1,2,3], [2,3,4]] v3=() v4="123") v4="123"
@seemasharma If you should not match empty parenthesis you can use [^\s=,]+=(?:\([^()]*\)|.+?)(?=,\s*[^\s=,]+=(?!\(\))|$) see regex101.com/r/G6aVAM/1 But note that Java regex does not support recursion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.