in reply to Replacing a given character starting with the xth occurence in a string
This prints:$s = 'abc nbc cbs fox hbo sho cnn rox'; ($orig, $num, $subst) = ('n', 2, '-'); $s =~ s/^(([^$orig]*$orig){$num})(.*)/$1/; ($t = $3) =~ s/$orig/$subst/g; $s .= $t; print $s
It works by cutting the string into two parts, applying the substitution to the second part (if there is one) and rejoining them.abc nbc cbs fox hbo sho cn- rox
|
|---|