in reply to Replacement Regex

You'll need code; famously, regexps won't do balance counting. Finite State Machines vs Push-down Automata.