$ perl -E 'grep { /^..(A|B)./ and push @f, $1 } @ARGV; say @f' xxBxx yyAyy BA $