in reply to Re: Does s/\Q$find\E/$replace/g really a plain string replace ?
in thread Does s/\Q$find\E/$replace/g really a plain string replace ?
$_ = 'foo bbbbbbbah bar'; $find = 'bah'; $replace = 'ah';
and
$_ = 'foo babab bar'; $find = 'bab'; $replace = 'xxx';
don't work very well.
Perhaps something like:
sub replace { die "Some helpful messsage" unless 3 == grep defined $_, @_[0..2]; my ($str,$find,$rep) = @_; my $i = 0; my $l = length($find); my @pos; while ($i < length($str)) { $i = index($str,$find,$i); last if $i < 0; push @pos, $i; $i += $l; } substr($str,$_,$l,$rep) for reverse @pos; return $str; }
|
|---|