A regex engine with (?R) recursion — equivalent to Python's regex module.
Plus an extended replacement syntax: #( … #) marks a recursive group, and %1 in the replacement is processed recursively.
#(X#) wraps a recursive capture group. The engine compiles this to ((?:(?R)|X-atom)+?), allowing balanced nested matches.
%1, %2, … reference the captures. Each substitution is itself processed by the same rule, so nested matches are unwrapped from the inside out.
Both the user-given examples ① and ② work. Note: (%1) with literal parens preserves them; use %1 alone to drop them.