perl中带有s命令的错误结果

时间:2013-12-03 12:22:40

标签: perl

您好我想找到&替换字符串的一些内容

$source_folder = "D:\workdir\Devunit\11.1\latest";
$desitnation_folder = "D:\workdir\Devunit\10.1.3_Super\latest_Super";
$src_file = D:\workdir\Devunit\11.1\latest\src\tcbom\userver\buslogic\framework\bomitemfactory.cpp;    

$dest_file = $src_file;
print " $dest_file \n";
$dest_file =~ s/$source_folder/$desitnation_folder/;
print " $dest_file \n";

两个打印结果显示

D:\workdir\Devunit\11.1\latest\src\tcbom\userver\buslogic\framework\bomitemfactory.cpp

请告诉我错误的地方

1 个答案:

答案 0 :(得分:3)

如果您不希望字符串中的任何内容变得特殊,请使用quotemeta,即按字面意思进行解释。以下内容适用于您:

$source_folder = q(D:\workdir\Devunit\11.1\latest);
$desitnation_folder = q(D:\workdir\Devunit\10.1.3_Super\latest_Super);
$src_file = q(D:\workdir\Devunit\11.1\latest\src\tcbom\userver\buslogic\framework\bomitemfactory.cpp);

$dest_file = $src_file;
print " $dest_file \n";
$dest_file =~ s/\Q$source_folder\E/$desitnation_folder/;
print " $dest_file \n";

您可能还想引用How do I match a regular expression that's in a variable?