my $text = qq{foo this is one example bar this is a line I don't care about foo here's another keeper! bar foo yet another bar and a line to reject}; foreach ($text =~ /^foo(.*)bar$/gm) { manipulate($_); } sub manipulate { print $_, "\n"; }