3

具体来说,有没有办法实现相当于

my $string = 'this is some example text';
my $match = qr/foobar/;
print 'success' if $string !~ $match;

只使用=~,不使用否定运算符?

具体来说,我需要确保字符串与提供的值不匹配,并且测试函数采用正则表达式对象,并将它们积极地应用于值。 该值根本不能出现在搜索的字符串中,这使前瞻和后瞻断言变得复杂。

像下面这样的东西可能是一个很好的测试:

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;

我怀疑有一种方法可以通过环视断言来做到这一点,但还没有弄明白。perl 特定的答案是可以接受的。

4

1 回答 1

5
my $string = 'this is some example text';
my $match = qr/^(?!.*foobar)/s;
print 'success' if $string =~ $match;
于 2012-10-15T18:44:18.613 回答