Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7869205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:14:36+00:00 2026-06-03T01:14:36+00:00

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

  • 0

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….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:14:38+00:00Added an answer on June 3, 2026 at 1:14 am

    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)";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In web application, i am using regular expression for date like dd-mm-yyyy format for
I want to match a date-format in PHP using a regular expression. To keep
I'm using a regular expression to check the format of a supplied date in
i am working with msbuild script.i want to comment particular line using regular expression
I read this and got really interested: Validating date format using regular expression so
I'm having some trouble using regular expression to get date in a string. Example
I want to validate mathematical expressions using regular expression. The mathematical expression can be
I am using a regular expression in javascript and want to do server side
I want to match D11-RONPLAYER_DEF_15_PO using this regular expression: D\[0-9]+-\[A-Z]*PLAYER_(DEF\[0-9]*)_(\[^_]+)_ but it does not
I would like to extract portion of a text using a regular expression. So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.