$string =~ m[ , # a comma \s+ # followed by at least one space ( # capture \d+ # one or more digits ) ]; #### #! perl -slw use strict; my @array = split '\n', <<'EOA'; Item1 - 2 foo, 2 bar Item2 - 0 foo, 1 bar Item3 - 1 foo, 3 bar Item4 - 1 foo, 2 bar EOA my @sorted = map{ # Strip the first five characters that we added earlier. substr $_, 5; } sort { # make the sort descending $b cmp $a } map { # print out the modified record to show what sprintf is doing print "before sort: '$_'"; $_; } map { ## try the regex and die displaying ## the failing record if it doesn't match die "Regex did not match: '$_'" unless m[,\s+(\d+)]; # It matched, so $1 contains the value to prepend. # pad to width 5. # use a bigger value here and above in the substr # if your numbers can be bigger than 99999. sprintf '%05d%s', $1, $_; } @array; print $/; print for @sorted; __END__ P:\test>test2 before sort: '00002Item1 - 2 foo, 2 bar' before sort: '00001Item2 - 0 foo, 1 bar' before sort: '00003Item3 - 1 foo, 3 bar' before sort: '00002Item4 - 1 foo, 2 bar' Item3 - 1 foo, 3 bar Item4 - 1 foo, 2 bar Item1 - 2 foo, 2 bar Item2 - 0 foo, 1 bar