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