use strict; use warnings; my $text = "and him him lad has him done and john has has"; while($text=~/(\w\w\w).*?\g1/g){ print "$1\n"; }