use strict; use warnings; my $pp = qr/(?: (\w) (?1) \g{-1} | \w? )/ix; local $/ = ''; while () { chomp; my ($header, @lines) = split "\n"; my $data = join '', @lines; print "$header\n$data\n"; while ($data =~ /(?=($pp))/g) { print "$-[0] - $1\n" if length($1) > 2; } }