http://www.perlmonks.org?node_id=111709


in reply to Pattern Finding

(In perl -de 17:)
DB<6> $x = 'helloworldhellohellohihellohiworldhi' DB<7> print "$1\n" while $x =~ /(\w+)(?=.*\1.*\1)/g hello o l hello h l l o hi h l

The single letters appear, of course, since they're repeated more than once. We can fix that by requiring \1 to be at least 2 characters long...

DB<8> print "$1\n" while $x =~ /(\w{2,})(?=.*\1.*\1)/g hello hello hi
Of course it still finds `world', which appears 4 times.

To autogenerate the regexp:

sub repeat_finder_re { my $n = shift; '(\w{2,})(?=' . ('.*\\1'x($n-1)) . ')' }