gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:49:26 25 4
gpt4 key购买 nike

具体来说,有没有办法实现等同于

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 的答案。

最佳答案

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

关于regex - 有没有办法使用正匹配正则表达式运算符对字符串进行负匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12901944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com