我知道除了使用正则表达式的给定字符之外,很容易匹配任何内容.
$text = "ab ac ad";
$text =~ s/[^c]*//g; # Match anything, except c.
$text is now "c".
Run Code Online (Sandbox Code Playgroud)
我不知道如何"除"字符串而不是字符.我怎么能"匹配任何东西,除了'ac'"?尝试[^(ac)]和[^"ac"]没有成功.
有可能吗?
以下内容解决了Bart K.中描述的第二种意义所理解的问题.
>> $text='ab ac ad';
>> $text =~ s/(ac)|./\1/g;
>> print $text;
ac
Run Code Online (Sandbox Code Playgroud)
另外,'abacadac'- >'acac'
应该注意的是,在大多数实际应用中,负面前瞻证明比这种方法更有用.
您可以根据您的目的轻松修改此正则表达式。
use Test::More 0.88;
#Match any whole text that does not contain a string
my $re=qr/^(?:(?!ac).)*$/;
my $str='ab ac ad';
ok(!$str=~$re);
$str='ab af ad';
ok($str=~$re);
done_testing();
Run Code Online (Sandbox Code Playgroud)