2011-05-23 28 views
0

我已經看到了這個問題的答案:Modify regex to match dates with ordinals "st", "nd", "rd", "th" 但我需要它的Perl格式。修改正則表達式來匹配日期與序號「st」,「nd」,「rd」,「th」

此外,這隻解析2004年1月1日。我需要一個正則表達式,可以解析2004年1月1日,2004年1月1日或2004年1月1日。我很新的正則表達式,所以不知道如何得到這個。

這僅適用於英文日期,並且需要適用於1月/ 1月。

任何信息都將有所幫助。

謝謝

回答

0

如何:

my @strs = ('1 jan 2004','1st Jan 2004','1st of Jan 2004'); 
for(@strs) 
{ 
    if(m/^\s*(\d+)(?:[a-z]{2})?\s+(?:of\s+)?([a-z]{3})\s+(\d{4})/i) 
    { 
     my($date,$month,$year) = ($1,$2,$3); 
     print "matched $date $month $year\n"; 
    } 
} 

這不驗證字符串是有效的日期(即一月的43次),但檢查可能 與$日期被添加, $月,$年變量。