use warnings; use strict; open my $fh, '<', 'file.txt' or die $!; { local $/ = 'CAS*'; while (<$fh>){ print $_ if /LX\*.*(?=00003$)/ms; } }