Date :: Parse绝对会成为你的答案的一部分 - 这是一个随机格式化的类似日期的字符串,并将实际可用日期排除在外。
问题的其他部分 - 文件名中其余的字符 - 非常不寻常,以至于您不太可能找到其他人为您打包了一个模块。
没有看到更多您的样本数据,它只能猜测,但我首先确定可能的或可能的“日期部分”候选人。
这是一个令人讨厌的蛮力示例,使用Date :: Parse(一个更智能的方法将使用正则表达式列表来尝试和识别日期位 - 我很高兴地刻录cpu周期以不觉得这么难虽然!)
!/usr/bin/perl
use strict;
use warnings;
use Date::Parse;
my @files=("Report Aug06.xls", "ReportAug2006", "Report 11th September 2006.xls",
"Annual Report-08-06", "End-of-month Report01-08-06.xls", "Report2006");
# assumption - longest likely date string is something like '11th September 2006' - 19 chars
# shortest is "2006" - 4 chars.
# brute force all strings from 19-4 chars long at the end of the filename (less extension)
# return the longest thing that Date::Parse recognises as a date
foreach my $file (@files){
#chop extension if there is one
$file=~s/\..*//;
for my $len (-19..-4){
my $string = substr($file, $len);
my $time = str2time($string);
print "$string is a date: $time = ",scalar(localtime($time)),"\n" if $time;
last if $time;
}
}
这有点类似于我到底是怎么做的,但我的时间更长,更丑陋,更可怕:)我现在不会提出这个问题,以防有人在那之前遇到问题,但似乎像一个滚动你自己的解决方案的东西...... – 2010-08-10 06:24:46
你的答案基本上是正确的;似乎没有任何图书馆这样做,你必须自己做:) – 2010-08-13 15:18:53