Regular Expression to match valid dates

I’m trying to write a regular expression that validates a date. The regex needs to match the following

  • M/D/YYYY
  • Single digit months can start with a leading zero (eg: 03/12/2008)
  • Single digit days can start with a leading zero (eg: 3/02/2008)
  • CANNOT include February 30 or February 31 (eg: 2/31/2008)

So far I have


This matches properly EXCEPT it still includes 2/30/2008 & 2/31/2008.

Does anyone have a better suggestion?

Edit: I found the answer on RegExLib


It matches all valid months that follow the MM/DD/YYYY format.

Thanks everyone for the help.

Regular expression to match valid namespace name

I thought this question was asked before but I tried Google but didn’t find an answer. Maybe I used wrong keywords. Is it possible to use regular expression to match valid C# namespace name? Thanks Up

Regular Expression to match dates in dd/mm/yy format and check for valid values

Does anyone have a regurlar expression available which only accepts dates in the format dd/mm/yy but also has strict checking to make sure that the date is valid, including leap year support? I am cod

What regular expression will match valid international phone numbers?

I need to determine whether a phone number is valid before attempting to dial it. The phone call can go anywhere in the world. What regular expression will match valid international phone numbers?

Regular expression to match google calendar event feed dates

I have been trying to match various dates from a google calendar xml feed in JavaScript but I am failing miserably. Currently I have this regular expression: /s(.*)/s Here are some of the examples o

How do I match valid words with a ruby regular expression

Using a ruby regular expression, how do I match all words in a coma separated list, but only match if the entire word contains valid word characters (i.e.: letter number or underscore). For instance,

Regular expression to match a valid absolute Windows directory containing spaces

I would like a regular expression to match a valid absolute Windows directory path, where directory names can contain spaces. Example matches: C:/pictures/holiday (without trailing backslash) C:/pictu

Valid regular expression

In order to check to see if a string is a valid regular expression I use the following piece of code: import re try: re.compile(‘(0*|2*)’) is_valid = True print(is_valid) except re.error: is_valid = F

regular expression matching dates

Need to match date in a user submitted string it should work with these different formats jan 1 2000 january 1 2000 jan. 1 2000 1/1/2000 2000 january how would you write this regular expression?

Regular Expression for date format validation [duplicate]

This question already has an answer here: Regular Expression to match valid dates 14 answers Regex to match Date 4 answers javascript – regular expression to validate date in mm/dd/yyyy form

Regular Expression Cant Match

I have the following string: 8 0 0 . Item s Payable in Connection w ith Loan I am trying to match it using the following regular expression: ^8//s*0//s*0//. What I believe this regular expression is s


Sounds like you’re overextending regex for this purpose. What I would do is use a regex to match a few date formats and then use a separate function to validate the values of the date fields so extracted.

Regex was not meant to validate number ranges(this number must be from 1 to 5 when the number preceding it happens to be a 2 and the number preceding that happens to be below 6). Just look for the pattern of placement of numbers in regex. If you need to validate is qualities of a date, put it in a date object js/c#/vb, and interogate the numbers there.

This is not an appropriate use of regular expressions. You’d be better off using


and then checking ranges in a higher-level language.

I know this does not answer your question, but why don’t you use a date handling routine to check if it’s a valid date? Even if you modify the regexp with a negative lookahead assertion like (?!31/0?2) (ie, do not match 31/2 or 31/02) you’ll still have the problem of accepting 29 02 on non leap years and about a single separator date format.

The problem is not easy if you want to really validate a date, check this forum thread.

For an example or a better way, in C#, check this link

If you are using another platform/language, let us know

If you’re going to insist on doing this with a regular expression, I’d recommend something like:

( (0?1|0?3| <...> |10|11|12) / (0?1| <...> |30|31) |
  0?2 / (0?1| <...> |28|29) ) 
/ (19|20)[0-9]{2}

This might make it possible to read and understand.

Perl expanded version

Note use of /x modifier.

        ( # 31 day months
          | ([13578])
          | (1[02])
          | ([0-2][0-9])
          | (3[01])
    | (
        ( # 30 day months
          | ([469])
          | (11)
          | ([0-2][0-9])
          | (30)
    | ( # 29 day month (Feb)
          | ([0-2][0-9])
    # year

  | ^/d{4}$ # year only



Maintainable Perl 5.10 version

      (?<month> (?&mon_29)) [//] (?<day>(?&day_29))
    | (?<month> (?&mon_30)) [//] (?<day>(?&day_30))
    | (?<month> (?&mon_31)) [//] (?<day>(?&day_31))
  (?<year> /d{4})

    (?<day_29> [0-2]?/d )
    (?<day_30> [0-2]?/d | 30 )
    (?<day_31> [0-2]?/d | 3[01] )

    (?<mon_29> 0?2 )
    (?<mon_30> 0?[469]   | (11) )
    (?<mon_31> 0?[13578] | 1[02] )

You can retrieve the elements by name in this version.

say "Month=$+{month} Day=$+{day} Year=$+{year}";

Perl 6 version

  $month := (/d{1,2})
  { $^month <= 12 or fail }
  $day := (/d{1,2})
    given( +$^month ){
      when any(qw'1 3 5 7 8 10 12') {
        $day <= 31 or fail
      when any(qw'4 6 9 11') {
        $day <= 30 or fail
      when 2{
        $day <= 29 or fail
      default { fail }
  $year := (/d{4})

Note this was quickly made and probably has a few bugs.

A slightly different approach that may or may not be useful for you.

I’m in php.

The project this relates to will never have a date prior to the 1st of January 2008. So, I take the ‘date’ inputed and use strtotime(). If the answer is >= 1199167200 then I have a date that is useful to me. If something that doesn’t look like a date is entered -1 is returned. If null is entered it does return today’s date number so you do need a check for a non-null entry first.

Works for my situation, perhaps yours too?

Here is the Reg ex that matches all valid dates including leap years. Formats accepted mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy format


courtesy Asiq Ahamed

if you didn’t get those above suggestions working, I use this, as it gets any date I ran this expression through 50 links, and it got all the dates on each page.

    var dtRegex = new RegExp(/[1-9/-]{4}[0-9/-]{2}[0-9/-]{2}/);
    if(dtRegex.test(date) == true){
        var evalDate = date.split('-');
        if(evalDate[0] != '0000' && evalDate[1] != '00' && evalDate[2] != '00'){
            return true;

To control a date validity under the following format :


I would recommand you tu use the following regular expression :



2016-02-29 | 2012-04-30 | 2019/09/31


2016-02-30 | 2012-04-31 | 2019/09/35

You can customise it if you wants to allow only ‘/’ or ‘-‘ separators. This RegEx strictly controls the validity of the date and verify 28,30 and 31 days months, even leap years with 29/02 month.

Try it, it works very well and prevent your code from lot of bugs !

FYI : I made a variant for the SQL datetime. You’ll find it there (look for my name) : Regular Expression to validate a timestamp

Feedback are welcomed 🙂

This regex validates dates between 01-01-2000 and 12-31-2099 with matching separators.

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

I landed here because the title of this question is broad and I was looking for a regex that I could use to match on a specific date format (like the OP). But I then discovered, as many of the answers and comments have comprehensively highlighted, there are many pitfalls that make constructing an effective pattern very tricky when extracting dates that are mixed-in with poor quality or non-structured source data.

In my exploration of the issues, I have come up with a system that enables you to build a regular expression by arranging together four simpler sub-expressions that match on the delimiter, and valid ranges for the year, month and day fields in the order you require.

These are :-



This will match anything that is not a word character, digit character, carriage return, new line or colon. The colon has to be there to prevent matching on times that look like dates (see my test Data)

You can optimise this part of the pattern to speed up matching, but this is a good foundation that detects most valid delimiters.

Note however; It will match a string with mixed delimiters like this 2/12-73 that may not actually be a valid date.

Year Values


This matches a group of two or 4 digits, in most cases this is acceptable, but if you’re dealing with data from the years 0-999 or beyond 9999 you need to decide how to handle that because in most cases a 1, 3 or >4 digit year is garbage.

Month Values


Matches any number between 1 and 12 with or without a leading zero – note: 0 and 00 is not matched.

Date Values


Matches any number between 1 and 31 with or without a leading zero – note: 0 and 00 is not matched.

This expression matches Date, Month, Year formatted dates


But it will also match some of the Year, Month Date ones. It should also be bookended with the boundary operators to ensure the whole date string is selected and prevent valid sub-dates being extracted from data that is not well-formed i.e. without boundary tags 20/12/194 matches as 20/12/19 and 101/12/1974 matches as 01/12/1974

Compare the results of the next expression to the one above with the test data in the nonsense section (below)


There’s no validation in this regex so a well-formed but invalid date such as 31/02/2001 would be matched. That is a data quality issue, and as others have said, your regex shouldn’t need to validate the data.

Because you (as a developer) can’t guarantee the quality of the source data you do need to perform and handle additional validation in your code, if you try to match and validate the data in the RegEx it gets very messy and becomes difficult to support without very concise documentation.

Garbage in, garbage out.

Having said that, if you do have mixed formats where the date values vary, and you have to extract as much as you can; You can combine a couple of expressions together like so;

This (disastrous) expression matches DMY and YMD dates


BUT you won’t be able to tell if dates like 6/9/1973 are the 6th of September or the 9th of June. I’m struggling to think of a scenario where that is not going to cause a problem somewhere down the line, it’s bad practice and you shouldn’t have to deal with it like that – find the data owner and hit them with the governance hammer.

Finally, if you want to match a YYYYMMDD string with no delimiters you can take some of the uncertainty out and the expression looks like this


But note again, it will match on well-formed but invalid values like 20010231 (31th Feb!) 🙂

Test data

In experimenting with the solutions in this thread I ended up with a test data set that includes a variety of valid and non-valid dates and some tricky situations where you may or may not want to match i.e. Times that could match as dates and dates on multiple lines.

I hope this is useful to someone.

Valid Dates in various formats

Day, month, year

month, day, year

Match both DMY and MDY

using whitespace as a delimiter

13 11 2001
11 13 2001
11 13 01 
13 11 01
1 1 01
1 1 2001

Year Month Day order

YYYYMMDD sortable format

Valid dates before Epoch

Valid date after 2038


Valid date beyond the year 9999


Dates with leading or trailing characters

12/10/2016  8:26:00.39

Times that look like dates

1:12:01 AM

Dates that runs across two lines




Invalid, corrupted or nonsense dates