Friday, May 08, 2009

Cool replace string without regex in perl

my $str = "C:\\app\\myapp\\backup\\db\\now_working";
print "\nstr = $str";
$replace_this = "myapp";
$with_this = "yourapp";
$str =~ s/\Q$replace_this\E/$with_this/;
print "\nstr = $str";

1 comment:

Anonymous said...

Cheeku,
I spent most of the day looking for a simple way to do string substitution in perl w/o regex. You made my day with the 's/\Q..\E//' notation. This is definitely a keeper.
Thank you.