Chinaunix

标题: 如何匹配得到最后一级目录名称? [打印本页]

作者: systemalgorithm    时间: 2007-10-09 09:55
标题: 如何匹配得到最后一级目录名称?
比如: 输入为:
/data/tmp/2007-10-21
/data/tmp/2007-10-21/
2007-10-21
2007-10-21/

都能得到"2007-10-21". 怎么匹配呢?
作者: flw    时间: 2007-10-09 10:05
perldoc File::Basename

  1. D:\MoChou>perl -MFile::Basename -le "print pack 'A30A*', $_, basename($_) for qw
  2. ( /data/tmp/2007-10-21 /data/tmp/2007-10-21/ 2007-10-21 2007-10-21/ )"
  3. /data/tmp/2007-10-21          2007-10-21
  4. /data/tmp/2007-10-21/         2007-10-21
  5. 2007-10-21                    2007-10-21
  6. 2007-10-21/                   2007-10-21

  7. D:\MoChou>
复制代码

作者: systemalgorithm    时间: 2007-10-09 10:10
But any other regrex without using "File::Basename"?
作者: flw    时间: 2007-10-09 10:13
原帖由 systemalgorithm 于 2007-10-9 10:10 发表
But any other regrex without using "File::Basename"?

你可以先告诉我为什么不用 File::Basename 吗?
作者: systemalgorithm    时间: 2007-10-09 10:20
use File::Basename;

$name = $ARGV[0];
$name =~ s/(.*)\//$1/;
$want = basename($name);
print "$want\n";

作者: systemalgorithm    时间: 2007-10-09 10:22
标题: 回复 #4 flw 的帖子
我忘了这个了 File::Basename          :-) 半天没想起来
作者: systemalgorithm    时间: 2007-10-09 13:51
my $name = ARGV[0];
$last=(split(/\//, $name))[-1];
print "$last\n";

[ 本帖最后由 systemalgorithm 于 2007-10-9 14:06 编辑 ]




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2