有没有办法使用正匹配正则表达式运算符对字符串进行否定匹配?

kbe*_*son 3 regex perl regex-negation regex-lookarounds

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

my $string = 'this is some example text';
my $match = qr/foobar/;
print 'success' if $string !~ $match;
Run Code Online (Sandbox Code Playgroud)

仅使用=〜,而不使用否定运算符?

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

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

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;
Run Code Online (Sandbox Code Playgroud)

我怀疑有一种方法可以通过环顾四周的断言来做到这一点,但还没有解决它.perl特定答案是可以接受的.

ike*_*ami 5

my $string = 'this is some example text';
my $match = qr/^(?!.*foobar)/s;
print 'success' if $string =~ $match;
Run Code Online (Sandbox Code Playgroud)