#counter is equal to the number of [] pairs while($counter) { if($content =~ /\[{(.*)?}\](.+)/) { print OUT "$counter\n"; print OUT "$1\n"; $content = $2; } #}]], "plt": --$counter; }