if ($data=~/(\D{3}) *?(\d{1,2}) *?(\d{4})/) # i.e. look for 3 non-digits, then any number of spaces, then either 1 or two digits, then any number of spaces, then 4 digits. (I wasn't sure how many spaces separated each part.) { $month= $datehash{$1},br; # datehash is a lookup for the month $day=$2; if ($day=~/\d{1}/) { $day='0'.$day; # i.e. add a '0' in front of days consisting of 1 digit, so 16 stays as 16, but 1 turns into 01 } $year=$3; $sortingdate=join '', $year, $month, $day; }