use warnings; use strict; $_ = "helloworldhellohellohihellohiworld"; my %pttrns; PTTRN: { if (/\G(.{2,})(?=.*?\1)/g) { $pttrns{$1}++; SKIP: { my $again; for my $pat (keys %pttrns) { if (substr($_, pos, length $pat) eq $pat) { pos() += length($pat); redo SKIP; } } } redo PTTRN; } } print "[$_]\n" for keys %pttrns; ~/tmp >./tst [world] [hello] [hi]