#!/usr/bin/env perl use strict; use warnings; local $/ = undef; my $string = ; while ($string =~ m{ \b #word boundary (\w+) #word (saved in $1) \b #word boundary (?= #Look ahead, but do not "eat up", all the following (.*) #Anything (and save in $2) \b #word boundary \1 #same word we already found \b #word boundary ) }sigx){ #allow . to match \n, case insensitive, progressive matching my ($word, $between) = ($1, $2); print "Between copies of '$word':\n"; print "="x35, "\n"; print "$between\n"; print "="x35, "\n\n"; } __DATA__ The quick brown fox jumps over the lazy dog, A fox stands by my dog over there. This text is over now.