my $lines; while ( <$fh> ) { $lines .= $_; if ( $lines =~ /($regex)/sm ) { my $newlines = $1 =~ tr/\n//; if ( $newlines ) { print "Match found on lines ", $. - $newlines, " through $.\n"; } else { print "Match found on line $.\n"; } $match = 1; $lines = $_; } }