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

perl, regex

Solution

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.

Problem

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 c. $text is now "c". ``` I don't know how to "except" strings instead of characters. How would I "match anything, except 'ac'" ? Tried [^(ac)] and [^"ac"] without success. Is it possible at all?

Original source

Related problems