use warnings; use strict; use feature 'say'; my $str = do { local $/; ; }; say join " " => grep { $_ if /fox/ .. /over/ } split /\n/, $str; __DATA__ the quick brown fox jumped over the unfortunate dog