... my ($fiop, $length, $data) = $string =~ m{ # end: start file tag \s* (.*?) # (baz) - note the "nongreedyness" .*? # end: file tag }x; print "$fiop, $length, $data\n"; ... #### ... (my $notags = $string) =~ s{}{}; print "$notags\n"; ...