Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How can I phrase a substitution in which I replace $a with $b only if $a isn't directly followed by $c?
  • Comment on Substitute $a with $b only when a is not followed by $c

Replies are listed 'Best First'.
Re: Substitute $a with $b only when a is not followed by $c
by japhy (Canon) on Jan 28, 2000 at 02:54 UTC
    You want to use a negative look-ahead assertion (as described in 'perlre'):
    $string = "123456123789"; $a = 123; $b = "abc"; $c = 456; $string =~ s/$a(?!=$c)/$b/;
    That sets string to "123456abc789". (?=...) is a positive look-ahead, (?!=...) is a negative look-ahead. (?<=...) is a positive look-behind, (?<!...) is a negative look-behind.