Regular expression for date range

If I have a directory structure like this

yyyy/dd/mm/<files> 

Is there a grep way to string in all files at a given time interval using a regular expression? For example, I have a time frame: 2010/12/25 - 2011/01/01, I need to grep all the files in the directories corresponding to the dates from December 25 to January 1

If I do this programmatically, is it better to iterate over the date range and grep files in each yyyy / dd / mm directory than use a regex for this? Or will it not affect?

+7
source share
1 answer

In your case, this is quite simple:

 \b(?:2010/12/(?:3[01]|2[5-9])|2011/01/01)\b 

will match a string containing a date in the range you specify. But usually regular expressions are not suitable for matching date ranges. This is always an opportunity, but rarely good.

For example, for the range 2003/04 / 25-2011 / 04/04 you get

 \b(?: 2003/04/(?:30|2[5-9])| 2003/(?:(?:0[69]|11)/(?:30|[12][0-9]|0[1-9])|(?:0[578]|1[02])/(?:3[01]|[12][0-9]|0[1-9]))| 2011/04/0[1-4]|2011/(?:02/(?:[12][0-9]|0[1-9])|0[13]/(?:3[01]|[12][0-9]|0[1-9]))| (?:2010|200[4-9])/(?:02/(?:[12][0-9]|0[1-9])|(?:0[469]|11)/(?:30|[12][0-9]|0[1-9])|(?:0[13578]|1[02])/(?:3[01]|[12][0-9]|0[1-9])) )\b 

If I had to do something similar (and could not use the creation dates in the file attributes), I would either use RegexMagic (to create a regular expression for the date range) and PowerGREP (to do grepping) if this is a one-time job, but they are available only on windows. If I had to do this more often, I would write a small Python script that looks at my directory tree, analyzes the date for each directory, checks to see if it is in a range, and then looks at the files in that directory.

+12
source

All Articles