my $text = 'this {{}}should be {{test {{ab{{c}}d}} zzzz}}good {{bw}}'; my $nested = 100; my $re = '{{(?:[^{}]+|'; $re .= $re x ($nested); $re .= ')*}}' x ($nested + 1); $text =~ s/$re//sg; print "$text\n"; __END__|OUTPUT: this should be good