I came up with a regex string that parses the given text into 3 categories:
- in parentheses
- in brackets
- neither.
Like this:
\[.+?\]|\(.+?\)|[\w+ ?]+
My intention is to use the outermost operator only. So, given a(b[c]d)e, the split is going to be:
a || (b[c]d) || e
It works fine given parentheses inside brackets, or brackets inside parentheses, but breaks down when there are brackets inside brackets and parentheses inside parentheses. For example, a[b[c]d]e is split as
a || [b[c] || d || ] || e.
Is there any way to handle this using regex alone, not resorting to using code to count number of open/closed parentheses? Thanks!
[[\]()]|[^[\]()]+(the brackets in question and anything else). then walk the matches, incrementing the relevant depth counters when encountering each bracket type.