Regular expression to match date range

Does anyone have any ideas on how and how to 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 racked my brains over how to come up with something, but I have nothing that is close. Examples on the Internet are only interested in checking if the date is in a specific format, but nothing about checking ranges.

The reason I noted C # in this is because it is not possible to do this in direct regex, and a range regex must be created manually for each individual case.

+4
source share
7 answers

Wouldn't it be easier to parse strings on a DateTime and compare them?

+16
source

I don’t think you need regular expressions to test the range. First check that the date with the regular expression is correct, then check if it falls within the given period.

+8
source

DONT ...

Use a regular expression to retrieve a date, and then compare it with a range. It will be much easier than creating a regular expression to match daterange ...

This is a classic example of the famous "... and then you have 2 problems";)

+6
source

That would be possible, but it would be a little difficult to write code that dynamically generates a regular expression.

For the range 11/23/2008 - 12/04/2010, you first divide it into three ranges for the first year, last year, and between years. Then you will divide each year into months with the same number of days in them. Then you divide the date ranges for each monthly length into dozens (for example, 01-09, 10-19, 20-28). From this you can create a regular expression, for example:

 ^(2[3-9]|30)/11/2008| (0[1-9]|[12]\d|3[01])/12/2008| (0[1-9}|1\d|2[0-8])/02/2009| (0[1-9]|[12]\d|3[01])/(0[13578]|1[02])/2009| (0[1-9]|[12]\d|30)/(0[469]|11)/2009| (0[1-9}|1\d|2[0-8])/02/2010| (0[1-9]|[12]\d|3[01])/0[13]/2010| (0[1-9]|1[0-2])/04/2010$ 
+4
source

As far as I know, this is impossible to do. You can try a two-step approach: - regexp find the date and save it in the date object - check if the found date is in your range

0
source

Take a look at DateTime.ParseExact to turn a string into a date, and then use them like regular DateTime ranges. A regular expression would be terrible if possible !!

0
source

do not check for correct dates

09/09/1984 - 04/30/2001

 ^\d\d/(09|1[012])/1984| \d\d/\d\d/(198[5-9]|199\d|2000)| \d\d/0[1-3]/2001| (0[1-9]|[12]\d|30)/04/2001$ 

11/23/2008 - 04/12/2010

 ^(2[3-9]|30)/11/2008| \d\d/12/2008| \d\d/\d\d/2009| \d\d/0[1-3]/2010| (0[1-9]|1[0-2])/04/2010$ 
-one
source

All Articles