REGEXVAULTv2.0
Finance/Card Numbers
Verified Safe

Card Expiry Date (MM/YY or MM/YYYY) Regex for Python

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

What this pattern does

This page provides a well-structured, multi-part regular expression for matching card expiry date (mm/yy or mm/yyyy), 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
# Card Expiry Date (MM/YY or MM/YYYY)
# ReDoS-safe | RegexVault — Finance > Card Numbers

import re

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

def validate_card_expiry_date_mmyy_or_mmyyyy(value: str) -> bool:
    return bool(card_expiry_date_mmyy_or_mmyyyy_pattern.fullmatch(value))

# Example
print(validate_card_expiry_date_mmyy_or_mmyyyy("01/24"))  # True

Test Cases

Matches (Valid)
Rejects (Invalid)
01/2400/24
12/2913/24
06/20251/24
01/209901/24/00
01-24
01/9
01/199

When to use this pattern

This pattern is drawn from the Finance > Card Numbers 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

A card expires at the end of the expiry month (not the 1st). 06/24 is valid until June 30, 2024 23:59:59. Do not reject on the expiry month's first day.

Technical Notes

Capture group 1: month (01-12), group 2: year (2-digit or 4-digit). 2-digit years in the range 20-99 map to 2020-2099. Cards expired but still within the grace period may still be valid — check against today's date at application level.

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