How to Write a ValidDate Regex in Python


The challenge

Your task is to write a regular expression (regex) that will match a string only if it contains at least one valid date, in the format [mm-dd] (that is, a two-digit month, followed by a dash, followed by a two-digit date, surrounded by square brackets).

You should assume the year in question is not a leap year. Therefore, the number of days each month should have are as follows:

    1. January – 31 days
    1. February – 28 days (leap years are ignored)
    1. March – 31 days
    1. April – 30 days
    1. May – 31 days
    1. June – 30 days
    1. July – 31 days
    1. August – 31 days
    1. September – 30 days
    1. October – 31 days
    1. November – 30 days
    1. December – 31 days

All text outside a valid date can be ignored, including other invalid dates.

Examples

"[01-23]" # January 23rd is a valid date
"[02-31]" # February 31st is an invalid date
"[02-16]" # valid
"[ 6-03]" # invalid format
"ignored [08-11] ignored" # valid
"[3] [12-04] [09-tenth]" # December 4th is a valid date

The solution in Python code

Option 1:

import re

valid_date = re.compile(r"\[("
    # Jan, Mar, May, Jul, Aug, Oct, Dec: 31 days
    "(0[13578]|1[02])-(0[1-9]|[12]\d|3[01])|"
    # Feb: 28 days
    "02-(0[1-9]|1\d|2[0-8])|"
    # Apr, Jun, Sep, Nov: 30 days
    "(0[469]|11)-(0[1-9]|[12]\d|30)"
    ")\]")

Option 2:

import datetime
from re import compile

class check_date(object):

    def __init__(self):
        self._rgxp = compile(r'(?P<date>\[\d{2}-\d{2}\])')

    def search(self, string):
        date = None
        search_date = self._rgxp.search(string)
        try:
            date = datetime.datetime.strptime(search_date.group('date'), "[%m-%d]")
        except BaseException:
            pass

        if date: return date.date()


valid_date = check_date()

Option 3:

valid_date = compile('\[((?!02-(?:29|30))(?:0[1-9]|1[012])-(?:0[1-9]|1[0-9]|2[0-9]|30)|(?:0[13578]|1[02])-31)\]')

Test cases to validate our solution

test.describe("Basic tests")
test.expect(valid_date.search("[01-23]")!=None, "January 23rd is a valid date")
test.expect(valid_date.search("[02-31]")==None, "February 31st is an invalid date")
test.expect(valid_date.search("[02-16]")!=None , "valid")
test.expect(valid_date.search("[ 6-03]")==None, "invalid format")
test.expect(valid_date.search("ignored [08-11] ignored")!=None, "valid")
test.expect(valid_date.search("[3] [12-04] [09-tenth]")!=None, "December 4th is a valid date")
test.expect(valid_date.search("[02-00]")==None, "invalid format")
test.expect(valid_date.search("[[[08-29]]]")!=None, "valid")
test.expect(valid_date.search("[13-02]")==None, "invalid format")
test.expect(valid_date.search("[02-[08-11]04]")!=None, "valid")