Purely from a syntax point of view, the following would work:
my @res = grep { !/$tmp/ } @array;
There is no good general way to negate a (Perl) regular expression. Negative lookahead/lookbehind like (?! and (?<! will easily fail for (say):
use 5.020; use Data::Dumper; my $tmp = qr/re/; my @positive = grep {/$tmp/} 'rerere', 'foo'; my @negative = grep {/(?!$tmp)/} 'rerere', 'foo'; say Dumper \@positive; # 'rerere' say Dumper \@negative; # 'rerere', 'foo'
In the past, whenever I wanted this I've used an external variable for the grep result like this:
use 5.020; use Data::Dumper; my $tmp = qr/re/; my $reverse = 0; # my @positive = grep {!! /$tmp/ xor $reverse } 'rerere', 'foo'; $reverse = 1; my @negative = grep {!! /$tmp/ xor $reverse } 'rerere', 'foo'; say Dumper \@positive; # 'rerere' say Dumper \@negative; # 'foo'
In reply to Re: Invert grep match with qr// variable
by Corion
in thread Invert grep match with qr// variable
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |