Specifically, is there a way to achieve the equivalent of
my $string = 'this is some example text';
my $match = qr/foobar/;
print 'success' if $string !~ $match;
By using only =~, and no negation operators?
Specifically, I need to ensure a string does NOT match a supplied value, and the testing function takes regex objects, and applies them positively to the value. The value must not occur in the searched string at all, complicating look-ahead and look-behind assertions.
Something like the following might be a good test:
my $string = 'this is some example text';
my $match =~ qr/foobar/;
# $negated_match contains $match, or some transformed variation of it
my $negated_match = qr/$YOUR_REGEX_HERE/;
die 'failure' if $string =~ $match;
print 'success' if $string =~ $negated_match;
I suspect there’s a way to do this with look-around assertions, but haven’t puzzled it out yet. A perl specific answer is acceptable.
1 Answer