$number_set = "6 7 8 9 10"; $number_set =~ s/^(\d+\b).*(\b\d+)$/$1-$2/ and print $number_set; 6-10