Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to match a date range

Tags:

c#

regex

Does anyone have any ideas on if/how one could create a regular expression to match a date in any given period?

Two examples:

23/11/2008 - 12/04/2010

//The expression would evaluate whether 16/04/2010 was in this range, and return false.
//The expression would determine whether 03/12/2009 was in this range, and return true.

and

01/09/1984 - 30/04/2001

//The expression would evaluate whether 16/04/1990 was in this range, and return true.
//The expression would determine whether 03/12/2009 was in this range, and return false.

I've been racking my brain on how to come up with something, but I've got nothing that comes close. Examples on the web are only interested in checking whether a date is in a particular format, but nothing about validating ranges.

The reason I tagged C# in this is because, this couldn't be done in straight regex, and the range regex would need to be built manually for each individual case.

like image 327
Dan Atkinson Avatar asked Nov 28 '22 04:11

Dan Atkinson


2 Answers

Wouldn't it be easier to parse the strings to DateTimes and comparing those?

like image 149
Joren Avatar answered Dec 15 '22 14:12

Joren


I don't think you should regular expressions for range checks. First check whether the date is valid with a regular expression, then check whether it falls in the given period.

like image 43
Eric Minkes Avatar answered Dec 15 '22 15:12

Eric Minkes