Matlab Regular Expression for datetime string
1 view (last 30 days)
Show older comments
Hi, I have string that contains time information. "this_file_05312016' here 05(month)31(day)2016(year) I am able to make it work for search January any day by using "01[01-31]+2016" Now I want to do search January - March, any day of 2016. I used [01-03][01-31]+2016 seems not working. What I might be missing? Thank you for your help.
Accepted Answer
Walter Roberson
on 17 Mar 2016
Least specific to dates:
0[1-3]\d\d2016
more specific:
0[1-3]([0-2][0-9]|3[01])2016
Even more specific:
(0[13]([0-2][0-9]|3[01])|02[0-2][0-9])2016
or more compactly,
(0[13]([0-2]\d|3[01])|02[0-2]\d)2016
extending the more specific to April or beyond would add more complexity. Extending the search to non-leapyears would add more complexity.
Often, the very first of those possibilities is good enough, if you already know that all the things that vaguely look like dates are going to be valid dates, and you are just concerned with selecting the right months. The more complex versions are suitable for the case where you might have mixed in other things that are not valid dates and you need to select only valid dates, such as if there was this_file_02302016 that you want to skip because of the invalid date, there never being a February 30th.
More Answers (0)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!