Logo

Programming-Idioms

History of Idiom 296 > diff from v7 to v8

Edit summary for version 8 by skeletonkey:
New Perl implementation by user [skeletonkey]

Version 7

2022-02-17, 18:00:12

Version 8

2022-02-18, 13:35:18

Idiom #296 Replace last occurrence of substring

Assign to x2 the value of string x with the last occurrence of y replaced by z.
If y is not contained in x, then x2 has the same value as x.

Idiom #296 Replace last occurrence of substring

Assign to x2 the value of string x with the last occurrence of y replaced by z.
If y is not contained in x, then x2 has the same value as x.

Variables
x2,x,y,z
Variables
x2,x,y,z
Extra Keywords
substring substitute
Extra Keywords
substring substitute
Code
my $x2 = $x;
$x2 =~ s/y([^y]*)$/z$1/;
Doc URL
https://perldoc.perl.org/functions/s
Demo URL
http://codepad.org/LwnULDDE