if($output[$x+2] !~ /.*match1.*/ and $output[$x+2] =~ /.*(match2).*/ and $output[$x] =~ /.*(match3).*/ and $output[$x+1] =~/.*(match4).*/){ #Then be able to access all matched variables }