use strict; use warnings; my $regex = qr| Primary\s\(Reporting\)\s Cp\s*:\s*((?:(?!Set).)*) # Now we can see what's happening Set\s*:\s*((?:(?!Car).)*) # but why are the look aheads needed? Car\s*:\s*((?:(?!Ref).)*) |six; while () { next if $_ !~ $regex; print; }