Reputation: 8897
I'm using ActiveState perl 5.12.4 on Windows 7. I'm trying to execute a search and replace …
print "selected dir: $selected_dir basedir: $baseTestDir\n";
$selected_dir =~ s/$baseTestDir//g;
Where $selected_dir = "\home\selenium\projects\myco\AutomatedTests\MyCliUSA\Critical Path\Live\G Sedan"
and $baseTestDir = "\home\selenium\projects\myco\AutomatedTests\MyCliUSA"
. However, after the search and replace statement, $selected_dir
is unchanged. How can I properly implement a search and replace here?
Upvotes: 0
Views: 208
Reputation: 385897
You didn't convert the text in $baseTestDir
into a regex pattern. This can be using quotemeta
.
my $base_test_dir_pat = quotemeta($base_test_dir);
$selected_dir =~ s/^$base_test_dir_pat//;
It's also accessible via \Q..\E
in double-quoted and similar string literals.
$selected_dir =~ s/^\Q$base_test_dir\E//;
A trailing \E
can be omitted.
$selected_dir =~ s/^\Q$base_test_dir//;
Upvotes: 4
Reputation: 3236
You need to escape the '\'s.
Do
$baseTestDir =~ s/\\/\\\\/g;
before the replacement.
Upvotes: -1
Reputation: 329
I would write the following to replace selected_dir with baseTestDir:
$selected_dir =~ s/$selected_dir/$baseTestDir/;
Upvotes: -2