如何在 Perl 字符串中取消转义反斜杠?
问题描述:
我需要将输入地址转换为指定格式
I need the to convert the input address to specified format
#!/usr/bin/perl
my $file_path ="\\\abc.com\a\t\temp\L\\";
#---- Help in this regex
$file_path =~ s//\//\/gi;
#---- Output format needed
#$file_path ="\\abc.com\a\t\temp\L\";
printf $file_path;
答
我猜你想规范化一个 UNC 路径,在这种情况下,开头的双 \
很重要,并且Ether 和 KennyTM 的答案产生了错误的结果.选择以下任一方法.
I am guessing you want to normalise a UNC path, in which case the double \
at the beginning is important to keep, and the answers from Ether and KennyTM produce wrong results. Pick either one of the methods below.
use File::Spec qw();
print File::Spec->canonpath('\\\abc.com\a\t\temp\L\\');
use URI::file qw();
print URI::file->new('\\\abc.com\a\t\temp\L\\', 'win32')->dir('win32');
__END__
\\abc.com\a\t\temp\L\