fork download
  1. #!/usr/bin/perl
  2.  
  3. # Idiom #296 Replace last occurrence of substring
  4.  
  5. $x = 'A BB CCC this DDD EEEE this FFF';
  6. $y = 'this';
  7. $z = 'that';
  8.  
  9. $x2 = $x;
  10. $pos = rindex $x, $y;
  11. substr($x2, $pos, length($y)) = $z
  12. unless $pos == -1;
  13.  
  14. print $x2;
  15.  
Success #stdin #stdout 0.01s 5552KB
stdin
Standard input is empty
stdout
A BB CCC this DDD EEEE that FFF