in reply to Efficient way to match and replace nested braces (etc.)
Have you considered using the core Perl module Text::Balanced?