use strict; use warnings; sub process_para { my ($para) = @_; if (my ($id) = $para =~ /^(\w+)/) { if ($id ne 'blabla5') { $para =~ s/^TEXT TO BE REMOVED\n//mg; } } print($para); } { my ($depth, $buf); while () { $buf .= $_; ++$depth if /\(\s*$/; --$depth if /^\s*\)\s*$/; #printf("[%d] %s", $depth, $_); if (!$depth) { process_para($buf); $buf = ''; } } die("Bad nesting\n") if $depth; } __DATA__