How to delete and change string in cell array?

1 次查看(过去 30 天)
Hi, I have a cell array with every cell containing a string for every day such as: 1313406900.Mon.Aug.15_11_15_00.GMT.2011.nordzee1.cx.plan.bar
I want to change the strings on my cell array so it only give me the date, as Aug-15-2011 on the example. How can I delete the part of the string I'm not interested in?
Thanks in advance, Sara

回答(2 个)

Azzi Abdelmalek
Azzi Abdelmalek 2016-6-6
编辑:Azzi Abdelmalek 2016-6-6
s={'1313406900.Mon.Aug.15_11_15_00.GMT.2011.nordzee1.cx.plan.bar';'1313406900.Mon.Jun.17_12_15_00.GMT.2011.nordzee1.cx.plan.bar'}
out=datestr(regexpi(s,'(?<=[a-z]+\.)([a-z]+\.\d+_\d+)','match','once'),'dd-mmm-yyyy')

Guillaume
Guillaume 2016-6-6
On the assumption that the first _ is always between the day and the year, and that the year is always 20xx, and that the month is always before a dot immediately before the day:
regexprep(yourcellarray, '.*?([^.]+)\.(\d+)_(\d+).*', '$1-$2-20$3')
will work. The regular expression has five parts:
  • .*? is intended to match everything before the month. It matches any number of characters but as few as possible so that the rest of the expression still matches
  • ([^.]+)\. is intended to capture the month. It matches the longest sequence of non-dot characters followed by a dot. The sequence of non-dot character is token number 1
  • (\d+)_ is intended to capture the day. It matches as many digits as possible followed by a _. The digit sequence is token number 2
  • (\d+) is intended to capture the year. It matches as many digits as possible. This is token number 3
  • .* matches the remainder of the string
Tokens 1,2 and 3 are simply strung together with a - in between and a 20 before token 3. The rest of the match is discarded.

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by