# 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; #### # Capture the first five words including whitespace. if ( $text =~ / (\S+ \s+ \S+ \s+ \S+ \s+ \S+ \s+ \S+ )/x ) { # The regex extracted the first five words $text = "$1 ..."; # Shrink any internal whitespace $text =~ s/\s+/ /g; } #### if ( $text =~ s/ (\S+ \s+ \S+ \s+ \S+ \s+ \S+ \s+ \S+ )/$1 .../x ) { # The match succeeded, shrink the white space $text =~ s/\s+/ /g; }