I want to separate a string into an array based on spaces, with the caveat that spaces within a pair of curly or square brackets should be ignored.
I was able to find some answers that are close to what I want here and here, but they don't handle brackets nested within other brackets.
How do I split this string:
foo bar["s 1"]{a:{b:["s 2", "s 3"]}, x:" [s 4] "} woo{c:y} [e:{" s [6]"}] [simple square bracket] {simple curly bracket}
Into this array?
["foo", "bar[\"s 1\"]{a:{b:[\"s 2\", \"s 3\"]}, x:\" [s 4] \"}", "woo{c:y}", "[e:{\" s [6]\"}]", "[simple square bracket]", "{simple curly bracket}"]
When using the regex from the first link, I modified the regular expression to work with square and curly brackets, and got the correct output for the simple, un-nested parts of the example, but not for the complex nested area. See here.
The second link's answers relied on JSON formatting with colons, and it doesn't apply because my input will not necessarily be valid JSON and it also doesn't have a similar character pattern to adapt the answer to.
According to a commenter, this may not possible to do with regular expressions. Even if that is the case, any way of splitting the string to achieve the desired result would be considered a correct answer.