I am trying to get code between two braces, but still pay attention to nesting. Say I have something like the below as input:
while (true) { [A]
dothis();
if (whattype() == "A") { [B]
doA();
if (other() == "dog") { [C]
doB();
} [D]
} [E]
if (other() == "cat") { [F]
doZ();
} [G]
} [H]
And I want to recursively loop each nesting layer:
while
- if
- if
- if
The current function takes the string, uses regex (\{([\s\S]*)\}
) to greedily find code between the first and final brace and does that again to its contents until there are no more braces in the string.
The problem is the regex does not work for blocks of code next to each other. The regex matches the text between B up until G. It should instead start at B and stop at E, then another block from F to G.
Edit: I may end up using something other than regex. Are there any suggestions on how to handle this?
For future readers:
What I found helpful was this answer from another SO question.