shanish
shanish

Reputation: 1984

regular expression for date with Starting and Ending date

I am using the regular expression of the date for the format "MM/DD/YYYY" like

"^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$"

its working fine, no problem....here I want to limit the year between "1950" to "2050", how to do this, can anyone help me....

Upvotes: 0

Views: 775

Answers (2)

mgnoonan
mgnoonan

Reputation: 7200

So the answer depends on how you want to accomplish the task.

Your current Regex search pattern is going to match on most dates in the format "MM/DD/YYYY" in the 20th and 21st century. So one approach is to loop through the resulting matches, which are represented as string values at this point, and parse each string into a DateTime. Then you can do some range validation checking.

(Note: I removed the beginning ^ and ending $ from your original to make my example work)

string input = "This is one date 07/04/1776 and this is another 12/07/1941. Today is 08/10/2019.";
string pattern = "(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d\\d";

List<DateTime> list = new List<DateTime>();
foreach (Match match in Regex.Matches(input, pattern))
{
    Console.WriteLine(match.Value);
    DateTime result;
    if (DateTime.TryParse(match.Value, out result))
    {
        if (result.Year >= 1950 && result.Year <= 2050)
        {
            list.Add(result);
        }
    }
}

Console.WriteLine("Number of valid dates: {0}", list.Count);

This code outputs the following, noting that 1776 is not matched, the other two dates are, but only the last one is added to the list.

12/07/1941
08/10/2019
Number of valid dates: 1

Although this approach has some drawbacks, such as looping over the results a second time to try and do the range validation, there are some advantages as well.

  • The built-in DateTime methods in the framework are easier to deal with, rather than constantly adjusting the Regex search pattern as your acceptable range can move over time.
  • By range checking afterward, you could also simplify your Regex search pattern to be more inclusive, perhaps even getting all dates.
  • A simpler Regex search pattern is easier to maintain, and also makes clear the intent of the code. Regex can be confusing and tricky to decipher the meaning, especially for less experienced coders.
  • Complex Regex search patterns can introduce subtle bugs. Make sure you have good unit tests wrapped around your code.

Of course your other approach is to adjust the Regex search pattern so that you don't have to parse and check afterwards. In most cases this is going to be the best option. Your search pattern is not returning any values that are outside the range, so you don't have to loop or do any additional checking at that point. Just remember those unit tests!

As @skywalker pointed out in his answer, this pattern should work for you.

string pattern = "(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19[5-9][0-9]|20[0-4][0-9]|2050)";

Upvotes: 6

skywalker
skywalker

Reputation: 23

year 1950-2050 both inclusive can be found using 19[5-9][0-9]|20[0-4][0-9]|2050

Upvotes: 2

Related Questions