Logo

Programming-Idioms

History of Idiom 296 > diff from v16 to v17

Edit summary for version 17 by programming-idioms.org:
[Ruby] Added Doc URL for gsub

Version 16

2022-06-14, 19:02:31

Version 17

2022-09-05, 20:12:16

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
x2 = x.gsub(/#{y}(?!.*#{y})/, z )
Code
x2 = x.gsub(/#{y}(?!.*#{y})/, z )
Comments bubble
Regex with negative lookahead.
Comments bubble
Regex with negative lookahead.
Doc URL
https://ruby-doc.org/core-3.1.2/String.html#method-i-gsub