How can I unescape backslashes in a Perl string?

后端 未结 3 1530
旧巷少年郎
旧巷少年郎 2021-01-29 00:09

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_         


        
3条回答
  •  死守一世寂寞
    2021-01-29 00:42

    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\
    

提交回复
热议问题