⭐ in reply to How do I replace a substring (if exists) with a different substring in a string?
You can use a s/// regex like this:
my $str = "I have a dream"; my $find = "have"; my $replace = "had"; $find = quotemeta $find; # escape regex metachars if present $str =~ s/$find/$replace/g; print $str;
The quotemeta lets you find strings that contain regex meta characters and the /g at the end of the s/// does all occurances.
cheers
tachyon
|
|---|