$a = '/etc/init/tree/errrocodr/a.txt'
我想将/ etc / init / tree / errrocodr /提取到$dir和a.txt到$file.我怎样才能做到这一点?
(编者注:原来的问题假定你需要一个正则表达式.)
解决方法
只需使用
Basename:
use File::Basename; $fullspec = "/etc/init/tree/errrocodr/a.txt"; my($file,$dir,$ext) = fileparse($fullspec); print "Directory: " . $dir . "\n"; print "File: " . $file . "\n"; print "Suffix: " . $ext . "\n\n"; my($file,$ext) = fileparse($fullspec,qr/\.[^.]*/); print "Directory: " . $dir . "\n"; print "File: " . $file . "\n"; print "Suffix: " . $ext . "\n";
您可以看到这返回您请求的结果,但它也能够捕获扩展名(在上面的后面部分):
Directory: /etc/init/tree/errrocodr/ File: a.txt Suffix: Directory: /etc/init/tree/errrocodr/ File: a Suffix: .txt