2015-05-18 43 views
-1

尋找從文件名解析日期的方法。文件名被命名的方式沒有約定,但可以在字符串中包含日期。如何從該文件名提取日期?解析文件名並得到日期

20151030Thisisafilename 

Thisisafilename20151030 

Thisisafilename2015-10-30 

千恩萬謝

編輯 我的問題是不同的,因爲我沒有定界符如 「_」

+0

你的字符串中有_always_只有addional'Thisisafilename'你'DateTime'旁邊字符串?只有這些格式? 'yyyyMMdd'和'yyyy-MM-dd'? –

+2

您是否嘗試過正則表達式?看起來是正確的方法。 https://xkcd.com/208/ – Juan

+0

http://stackoverflow.com/questions/16222693/get-date-from-string,http://stackoverflow.com/questions/14917203/finding-if-a-string -contains-a-date-and-time,http://stackoverflow.com/questions/18381997/finding-datetime-from-a-string-c-sharp – CodeCaster

回答

1

這個正則表達式應該符合:

\d{4}-?\d{2}-?\d{2} 

它查找序列8位數字,可選用兩個破折號分隔。

它適用於以下格式:如果你想有一個「所有

  • 2015年12月31日
  • 201512-31
  • 2015年至1231年

或沒有辦法「,這應該工作:

(\d{4}-\d{2}-\d{2}|\d{8}) 

,對於作品:

  • 2015年12月31日
+0

謝謝,幫了很多 – developer9969