$/ = undef; while (<>) { print "\n$ARGV\n", / (.+?) ^.*^ (.+?) \z/xms } #### $dtrx = qr/[Dd][Tt]=(\d{8}\.\d{6})/; $/ = undef; while (<>) { ($aa, $z) = /$dtrx # get the first one (?: # and maybe, .* # after maybe many lines, ^.*? $dtrx # the first one on the last line )? # that has one /mxs; $aa or next; $z ||= ''; # or '' if no more print "$ARGV\t $aa -- $z\n"; }