if ( /^Subject:\D+(\d+)/ ){ # if you can be sure of the '#' before the digits, use this # if ( /^Subject:.*#(\d+)/ ) not that it buys you much push @subject, $1; next; } # if /subject ...