Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Does s/\Q$find\E/$replace/g really a plain string replace ?

by !1 (Hermit)
on Oct 02, 2004 at 21:57 UTC ( #395932=note: print w/replies, xml ) Need Help??


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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://395932]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2021-09-28 17:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?