#!/usr/bin/perl use strict; use warnings; my %uniq=(); my $string = 'ACRMGAHKMAHGTXX'; substr($string, $_, 10 ) =~ m[([AGTC].{0,8}[AGTC])] and ++$uniq{ $1 } for 0 .. length( $string )-1; for my $key (keys %uniq){ print $key, "\n"; } #above code outputs the following: GAHKMAHG CRMGAHKMA AHGT AHKMAHGT GT GAHKMAHGT ACRMGAHKMA #and I only want the following: ACRMGAHKMA GAHKMAHGT