my $area = $tmp; # anchor the pattern at the start of the line using ^ # then look for the third clump of characters and pick everything through up to the hyphen. # the ? turns off greedy matching, so you do not get messed up by duplicate occurrences of -dpt on the same line. $area =~ s/^.*\s+.*\s+(.*?)-dpt/\1/; return $area;