Skip to content

Patterns

Compiled regex patterns for the two supported formats, exported from the top level module.

They validate the shape of a string, not whether the week really existed: 2023-W53 matches ISOWEEK_PATTERN even though 2023 had only 52 weeks. See why validation is stricter than the regex for the reasoning, and working with Pydantic for how to choose between the two.

from iso_week_date import ISOWEEK_PATTERN, ISOWEEKDATE_PATTERN

print(bool(ISOWEEK_PATTERN.match("2023-W01")))
print(bool(ISOWEEK_PATTERN.match("2023-W01-1")))  # a week date, not a week
print(bool(ISOWEEKDATE_PATTERN.match("2023-W01-1")))
print(bool(ISOWEEK_PATTERN.match("2023-W53")))  # shape is valid, the week is not
True
False
True
True

Each pattern captures its components as groups:

match = ISOWEEKDATE_PATTERN.match("2023-W01-1")

print(match.groups())
('2023', 'W01', '1')

iso_week_date.ISOWEEK_PATTERN module-attribute

ISOWEEK_PATTERN: Final[Pattern[str]] = re.compile(f'^{YEAR_MATCH}-{WEEK_MATCH}$')

iso_week_date.ISOWEEKDATE_PATTERN module-attribute

ISOWEEKDATE_PATTERN: Final[Pattern[str]] = re.compile(f'^{YEAR_MATCH}-{WEEK_MATCH}-{WEEKDAY_MATCH}$')