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

前端 未结 6 1548
别跟我提以往
别跟我提以往 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:43

    The following solves the question as understood in the second sense described in Bart K. comment:

    >> $text='ab ac ad';
    >> $text =~ s/(ac)|./\1/g;
    >> print $text;
    ac
    

    Also, 'abacadac' -> 'acac'

    It should be noted though that in most practical applications negative lookaheads prove to be more useful than this approach.

提交回复
热议问题