use strict; use warnings; use Data::Dumper; my @strings = qw(exception:tex exception:mex exception:mex asdf tex:exception:mex asdf exception:mex asdf asdf exception:mex); foreach (1..4) { my @filtered = grep { pos = 0; /exception:/g && !/\Gtex/ } @strings; print Dumper(\@filtered); }