I have some strings that correspond to words separated by spaces and that can be grouped using parenthesis. I also have an array of strings that represent the groups present in the string (not necessarily in the same order).
Here is an example :
myString = "zzz AND (aaa AND (bbb AND ccc OR ddd) ) OR eee AND \"fff fff\" AND (ggg)"
myGroups = ["(ggg)", "(aaa AND (bbb AND ccc OR ddd) )"]
I want to split that query on spaces that are not inside quotes and that are not inside parenthesis. The output of the split on the previous string would be as follow :
["zzz", "AND", "(aaa AND (bbb AND ccc OR ddd) )", "OR", "eee", "AND", "\"fff fff\"", "AND", "(ggg)"]
I succeeded to split on spaces that are not inside quotes using the regex
\s(?=(?:[^'"`]*(['"`])[^'"`]*\1)*[^'"`]*$)
But I am struggling with adding the notion of groups in this.
Note :
-
myString
may contain nested groups, but I only want to split on the outer one - No need to handle cases where parenthesis syntax is wrong