use strict; my $text = do { local $/; <> }; my $date_rgx = qr/\d{4}-\d{2}-\d{2}/; my ( $date, $text ) = ( '', '' ); for ( split /\n($date_rgx)/, $text ) { # capture the date (but not the preceding \n) if ( /^$date_rgx$/ ) { $date = $_; } elsif ( /\bkeyword\b/ ) { print "$date$_\n"; } }