REGEXVAULTv2.0
Finance/Financial Formats
Verified Safe

Accounting Period (YYYY-MM) Regex for Python

/^((?:19|20)[0-9]{2})-(0[1-9]|1[0-2])$/

What this pattern does

This page provides a well-structured, multi-part regular expression for matching accounting period (yyyy-mm), ported and verified for Python. Financial data validation has zero tolerance for false negatives — a missed invalid entry can corrupt downstream calculations. The snippet below is ready to drop into your Python project — whether you're validating in a Django view, a FastAPI endpoint, or a standalone data processing script.

Python Implementation

Python
# Accounting Period (YYYY-MM)
# ReDoS-safe | RegexVault — Finance > Financial Formats

import re

accounting_period_yyyymm_pattern = re.compile(r'^((?:19|20)[0-9]{2})-(0[1-9]|1[0-2])$')

def validate_accounting_period_yyyymm(value: str) -> bool:
    return bool(accounting_period_yyyymm_pattern.fullmatch(value))

# Example
print(validate_accounting_period_yyyymm("2024-01"))  # True

Test Cases

Matches (Valid)
Rejects (Invalid)
2024-012024-00
2024-122024-13
1999-0624-01
2025-032024/01
2024-1
2024-012

When to use this pattern

This pattern is drawn from the Finance > Financial Formats category and carries a ReDoS-safe certification. That matters for Python developers because particularly important in Python web servers where CPU-bound regex operations can stall concurrent request handling. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.

Common Pitfalls

Fiscal year start varies by company and jurisdiction: US government FY starts October, Singapore FY starts April for many companies. Store fiscal year alongside calendar period.

Technical Notes

Capture group 1: year (1900-2099), group 2: month (01-12). Accounting periods usually follow calendar months but some companies use fiscal months not aligned to calendar months.

Have a pattern that belongs in the vault?

Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.

Submit a Pattern