MATLAB Answers

How do I get the date out of a filename using regexp?

4 ビュー (過去 30 日間)
Shayma Al Ali
Shayma Al Ali 2019 年 11 月 21 日
Answered: Stephen Cobeldick 2019 年 11 月 21 日
Currently, I have a list of files that are saved as the following 'xxx_2014_06_03_00_00_01'. I'd like to extract the date in the file name using regexp. Currently, my code is:
fdate=regexp(fList(i).name,'_/d+','once')
dt=datetime(fdate,'InputFormat','dd-MM-yyyy HH:mm:ss')
However, whenever I test it out, fdate is always returned as empty. I'm extremely confused about regexp and how to use it.
Thanks!

  0 件のコメント

サインイン to comment.

採用された回答

Star Strider
Star Strider 2019 年 11 月 21 日
There are likely several ways to approach this. I would do it a bit differently:
str = 'xxx_2014_06_03_00_00_01';
fdate = regexp(str, '\d*','match');
datev = str2double(fdate);
dt=datetime(datev)
producing (here):
dt =
datetime
03-Jun-2014 00:00:01
I left these as separate lines to demonstrate how it works. The str2double call could be inserted into the datetime call.

  0 件のコメント

サインイン to comment.

More Answers (2)

Jeremy Marcyoniak
Jeremy Marcyoniak 2019 年 11 月 21 日
編集済み: Jeremy Marcyoniak 2019 年 11 月 21 日
str = 'xxx_2014_06_03_00_00_01';
id = regexp(str,'\d')
Returns:
id =
5 6 7 8 10 11 13 14 16 17 19 20 22 23
These are the indices of str that contain numeric digits. If you know that the 'xxx' part of the filename will not contain numeric digits, then you could then go ahead and say
year = str2double(str(id(1:4)));
month = str2double(str(id(5:6)));
etc.
Alternatively, you could use 'strtok' and pass it the underscore character as a delimiter to get a string of the datetime and convert those to integers. This is likely less efficient, though.

  0 件のコメント

サインイン to comment.


Stephen Cobeldick
Stephen Cobeldick 2019 年 11 月 21 日
In one line without intermediate indices or double vector:
>> str = 'xxx_2014_06_03_00_00_01';
>> dtm = datetime(regexp(str,'(\d+_?){6}','match','once'),'InputFormat','yyyy_MM_dd_HH_mm_ss')
dtm =
03-Jun-2014 00:00:01

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。

タグ


Translated by