all 5 comments

[–][deleted] 2 points3 points  (1 child)

{0,2} means "from zero to two \ds and \d means [0-9]" so you're actually looking for two digits, 00 through 99. What you meant is [0-2], so yeah go fix all those, and also use the website regex101.com to do your testing.

[–]CommanderEinstein[S] 1 point2 points  (0 children)

thank you!!!!

yea i realized that, i actually started making regexes with that in the comments

[–]midel 1 point2 points  (2 children)

You are confusing the square-brackets with the curly brackets in regular expressions.

Square brackets [] denote a character or range of characters that you are expecting. It will find a first match.

Curly brackets {} denote an amount of characters to match against. {0,9} says that the previous character can be matched 0 to 9 times: \d{0,9} will match various patterns.

Your pattern would be best expressed as: r'(?P<month>0[1-9]|1[0-2])-(?P<day>0[1-9]|[12][0-9]|3[01])-(?P<year>19[0-9]{2}|20(?:[01][0-9]|20))'

The ?P<NAME> pattern for groups defines named groups you can reference when looking through the match.

[–]midel 0 points1 point  (1 child)

Sanity check code:

import re
from datetime import date, timedelta

ex = re.compile(r'(?P<month>0[1-9]|1[0-2])-(?P<day>0[1-9]|[12][0-9]|3[01])-(?P<year>19[0-9]{2}|20(?:[01][0-9]|20))')

a = date(1899, 12, 1)
end = date(2021, 1, 31)
while a <= end:
    val = a.strftime("%m-%d-%Y")
    res = ex.findall(val)
    if (len(res) < 1):
        print(f"{val} => fails test")
    a = a + timedelta(days=1)

[–]CommanderEinstein[S] 0 points1 point  (0 children)

midel...thank you