需要使用正則表達式或其他方法將UNIX路徑轉換爲DOS路徑。如何將UNIX路徑轉換爲DOS路徑
我
C:/My Document/Photo.gif
極品
C:\My Document\Photo.gif
需要使用正則表達式或其他方法將UNIX路徑轉換爲DOS路徑。如何將UNIX路徑轉換爲DOS路徑
我
C:/My Document/Photo.gif
極品
C:\My Document\Photo.gif
這是正則表達式
s/\//\\/g
首先,你通常不需要做映射,除非你打算通過cmd.exe
調用的東西; Windows API完全滿意於斜槓而不是文件名中的反斜槓(但cmd.exe
堅稱斜線啓動了一個選項)。
然後,直接回答你的問題也許是:
my $file = "C:/My Documents/Photo.gif";
$file =~ s{/}{\\}g;
print "$file\n";
有點多餘,但你可以使用TR,而不是一個正則表達式:
tr!\\!/!;
#!/usr/bin/perl
use strict; use warnings;
use File::Spec::Win32;
print File::Spec::Win32->canonpath('C:/My Document/Photo.gif'), "\n";
這不適合我,但Jonathan Leffler的版本使用{和}作爲分隔符。 – 2014-03-12 15:31:17
@WarrenP,效果很好: `$ echo「C:/ My Document/Photo.gif」| sed's/\ // \\/g'`生成 `C:\ My Document \ Photo.gif` – zhekaus 2017-12-12 06:34:02