$foo = "{john{david{mary{{{{martin}"; 1 while $foo =~ s/{([{}])/{empty$1/g; print "$foo\n";