我正在接受一个变量
$var2="\\hydfs00\PUBLIC1\DEV\pkumar\ITT_TEST_BUILD\CS2.1_PROD_TEST_40550_LG_LC_Java_sp36_obfuscated";
并希望将其更改为
$var2="//hydfs00/PUBLIC1/DEV/pkumar/ITT_TEST_BUILD/CS2.1_PROD_TEST_40550_LG_LC_Java_sp36_obfuscated";
我正在使用语法$var2 =~ s/\\/\//g;
进行更改。但输出结果如/hydfs00PUBLIC1DEVpkumarITT_TEST_BUILDCS2.1_PROD_TEST_40550_LG_LC_Java_sp36_obfuscated
请帮帮我。
答案 0 :(得分:8)
"\\hydfs00\PUBLIC1\DEV\pkumar\ITT_TEST_BUILD\CS2.1_PROD_TEST_40550_LG_LC_Java_sp36_obfuscated"
是一个错误:
Unrecognized escape \P passed through at …
Unrecognized escape \D passed through at …
Unrecognized escape \p passed through at …
Unrecognized escape \I passed through at …
Unrecognized escape \C passed through at …
评估为\hydfs00PUBLIC1DEVpkumarITT_TEST_BUILDCS2.1_PROD_TEST_40550_LG_LC_Java_sp36_obfuscated
。
您希望single-quote the string以限制反斜杠转义为反斜杠和单引号。 '\\hydfs00\PUBLIC1\DEV\pkumar\ITT_TEST_BUILD\CS2.1_PROD_TEST_40550_LG_LC_Java_sp36_obfuscated'
评估为\hydfs00\PUBLIC1\DEV\pkumar\ITT_TEST_BUILD\CS2.1_PROD_TEST_40550_LG_LC_Java_sp36_obfuscated
。
让我们在开始时正确地逃避反斜杠,这样我们得到两个反斜杠。 '\\\\hydfs00\PUBLIC1\DEV\pkumar\ITT_TEST_BUILD\CS2.1_PROD_TEST_40550_LG_LC_Java_sp36_obfuscated'
评估为\\hydfs00\PUBLIC1\DEV\pkumar\ITT_TEST_BUILD\CS2.1_PROD_TEST_40550_LG_LC_Java_sp36_obfuscated
。这是你想要的字符串。
现在我们可以operate了解价值。
my $var = '\\\\hydfs00\PUBLIC1\DEV\pkumar\ITT_TEST_BUILD\CS2.1_PROD_TEST_40550_LG_LC_Java_sp36_obfuscated';
$var =~ s{ \\ }{/}gx; # replace any single backslash with a single slash
# $var now has the value
# //hydfs00/PUBLIC1/DEV/pkumar/ITT_TEST_BUILD/CS2.1_PROD_TEST_40550_LG_LC_Java_sp36_obfuscated
这是所有新手的东西。转到learn the language,read a book or two。