kazuwal
kazuwal

Reputation: 1091

Regex match between specified dates?

I have the following date String format, taken from a large dataset:

Wed Oct 05 19:26:09 BST 2014

I need to extract the dates between 15th October 09:00 and 23rd October 11:00

I currently have this regex: (Oct\W\d\d\W\d\d\W\d\d\W\d\d) which matches: Oct 05 19:26:09

How could I alter it to match the date and time ranges I have specified?

Upvotes: 1

Views: 1390

Answers (2)

SERPRO
SERPRO

Reputation: 10067

Well as @Maciej Baranowski said.. you shouldn't do this with RegExp. But just in case you must to, and there's no other alternative.. Here is the Regular expression for that condition:

EDIT: New answer based on OP comments. Only match dates in between 15th and 23rd of October and in between 9:00:00 and 11:00:00

#Oct\W(?:1[5-9]|2[0-3])\W(?:(?:0?9|10):\d{2}:\d{2}|11:00:00)#

You can see it working here

Upvotes: 2

Maciej Baranowski
Maciej Baranowski

Reputation: 620

I don't think it's a good approach to try to find a range of dates with single regex - it would surely be complicated and difficult to change and reuse in future. Wouldn't it be better to extract number of day in month to a variable in programming language you use? Dealing with comparisons would be trivial then. Of course it depends on the context of your problem

Upvotes: 2

Related Questions