use strict; use warnings; my $word = "zezlze"; my $dictword = "zezezd"; my @pairs = $word =~ /(?=(..))/g; my %count; $count{$_}++ foreach (@pairs); my $matcher = qr/(?=(@{[join "|", @pairs]}))/; my %seen; my $matches = 0; my @matches; foreach my $match ($dictword =~ /$matcher/g) { if ($seen{$match}++ < $count{$match}) { $matches ++; push @matches, $match; } } print "Matches: @matches\n"; print "$matches $dictword \n"; #### Matches: ze ez ze 3 zezezd