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; }