use strict; use warnings; while ( ) { if ( /(^foo.*bar$)|(^foo)|(bar$)/ ){ print $1 ? "BOTH FOO AND BAR: $_" : $2 ? "ONLY FOO: $_" : "ONLY BAR: $_"; } else { print "NOT EITHER: $_" } } __DATA__ foo baz foo bar bar fu bar fuz foo on you bar