... my @nums = 1 .. 30; my $reg = qr{^(\d+)$ (??{ $1 % 3 ? '(?!)' : '' })}x; print join ',', grep /$reg/, @nums; ...