use strict; my $test = "alpha beta beta gamma gamma gamma delta"; # /(\b\w+\b)/ matches a single word : print "Word in string : $1\n" while $test =~ /(\b\w+\b)/g; # but we want such a word, followed by (whitespace and then) the word again : print "Double word : $1\n" if $test =~ /(\b\w+\b)\s*\1/; # but we not only want to catch duplicates, we also want to catch # multiple repetitions : print "Double or more word : $1\n" if $test =~ /(\b\w+\b)(\s*\1)+/; # And since we're throwing it away anyway, there's no need to actually # capture stuff into $2 : print "Double or more word : $1\n" if $test =~ /(\b\w+\b)(?:\s*\1)+/; # Now, here's the whole RE to seek out repeating words # and collapse them into one word. $test =~ s/(\b\w+\b)(?:\s*\1)+/$1/g; print $test;