#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11121725 use warnings; use List::Util qw( uniq ); my $text = "and him him lad has him done and john has has"; print "$_\n" for uniq $text =~ /(\b\w+\b)(?=.*?\b\1\b)/g;