Home / dating mak vcd / Regular expression validating date

Regular expression validating date

Because of this, you have to choose how simple or how accurate you want your regular expression to be.If you already know your subject text doesn’t contain any invalid dates, you could use a trivial regex such as cannot repeat more than two times or fewer than two times.The only difference between the two forms is readability. The final two solutions allow all of the date formats, just like the first two examples.The difference is that the last two use an extra level of alternation to restrict the dates to 12/31 and 31/12, disallowing invalid months, such as 31/31., for example.

You could use the date validation functions from c# or It's not that Reg Ex cannot be used, but it would be similar to re-inventing the wheen.

The other issue is that regular expressions don’t deal directly with numbers. That’s because the characters for the digits 0 through 9 occupy consecutive positions in the ASCII and Unicode character tables.

You can’t tell a regular expression to “match a number between 1 and 31”, for instance. See Chapter 6 for more details on matching all kinds of numbers with regular expressions.

Recipe 4.5 shows how to validate traditional date formats more accurately.

Recipe 4.6 shows how to validate traditional time formats.


  1. Regular Expression to This regex will match a string as a date in the formats M/D/YY, M/D/YYY, MM/DD/YY, and MM/DD/YYYY. It does not correct for leap year.

Leave a Reply

Your email address will not be published. Required fields are marked *