# Capture the first five words including whitespace. ($text) = $text =~ / (\S+ \s+ \S+ \s+ \S+ \s+ \S+ \s+ \S+ )/x; # Shrink any internal whitespace $text =~ s/\s+/ /g;