my %matches; while ($str =~ /([abcd]{8,13})/g) { $matches{$1}++; } use Data::Dumper; print Dumper \%matches;