Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

how can i convert backward slashes to forward slashes in perl

i am taking one variable $var2="\hydfs00PUBLIC1DEVpkumarITT_TEST_BUILDCS2.1_PROD_TEST_40550_LG_LC_Java_sp36_obfuscated";

and want to change it to

$var2="//hydfs00/PUBLIC1/DEV/pkumar/ITT_TEST_BUILD/CS2.1_PROD_TEST_40550_LG_LC_Java_sp36_obfuscated";

I'm using syntax $var2 =~ s/\///g; for changing this. but the output is coming like /hydfs00PUBLIC1DEVpkumarITT_TEST_BUILDCS2.1_PROD_TEST_40550_LG_LC_Java_sp36_obfuscated

please help me out.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

"\hydfs00PUBLIC1DEVpkumarITT_TEST_BUILDCS2.1_PROD_TEST_40550_LG_LC_Java_sp36_obfuscated" is a mistake:

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 …

It evaluates to hydfs00PUBLIC1DEVpkumarITT_TEST_BUILDCS2.1_PROD_TEST_40550_LG_LC_Java_sp36_obfuscated.

You want to single-quote the string in order to restrict backslash-escaping to backslashes and single quotes only. '\hydfs00PUBLIC1DEVpkumarITT_TEST_BUILDCS2.1_PROD_TEST_40550_LG_LC_Java_sp36_obfuscated' evaluates to hydfs00PUBLIC1DEVpkumarITT_TEST_BUILDCS2.1_PROD_TEST_40550_LG_LC_Java_sp36_obfuscated.

Let's properly escape the backslashes at the beginning so we get two backslashes. '\\hydfs00PUBLIC1DEVpkumarITT_TEST_BUILDCS2.1_PROD_TEST_40550_LG_LC_Java_sp36_obfuscated' evaluates to \hydfs00PUBLIC1DEVpkumarITT_TEST_BUILDCS2.1_PROD_TEST_40550_LG_LC_Java_sp36_obfuscated. This is the string you intended.

Now we can operate on the value.

my $var = '\\hydfs00PUBLIC1DEVpkumarITT_TEST_BUILDCS2.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

This is all newbie stuff. Go learn the language, read a book or two.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...