How can I match strings that don't match a particular pattern in Perl?

前端 未结 6 1525
别跟我提以往
别跟我提以往 2021-02-06 00:25

I know that it is easy to match anything except a given character using a regular expression.

$text = \"ab ac ad\";
$text =~ s/[^c]*//g; # Match anything, except         


        
6条回答
  •  醉梦人生
    2021-02-06 00:42

    You can easily modify this regex for your purpose.

    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();
    

提交回复
热议问题