Japanese Imperial Era Year (Reiwa/Heisei/Showa) Regex for Python
/^(?:(令和|R)((?:[1-9]|[1-9][0-9]))|(平成|H)((?:[1-9]|[12][0-9]|3[01]))|(昭和|S)((?:[1-9]|[12][0-9]|3[0-9]|6[0-4])))年(0?[1-9]|1[0-2])月(0?[1-9]|[12][0-9]|3[01])日$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching japanese imperial era year (reiwa/heisei/showa), ported and verified for Python. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. 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
# Japanese Imperial Era Year (Reiwa/Heisei/Showa)
# ReDoS-safe | RegexVault — Localization > Date Formats
import re
japanese_imperial_era_year_reiwaheiseishowa_pattern = re.compile(r'^(?:(令和|R)((?:[1-9]|[1-9][0-9]))|(平成|H)((?:[1-9]|[12][0-9]|3[01]))|(昭和|S)((?:[1-9]|[12][0-9]|3[0-9]|6[0-4])))年(0?[1-9]|1[0-2])月(0?[1-9]|[12][0-9]|3[01])日$')
def validate_japanese_imperial_era_year_reiwaheiseishowa(value: str) -> bool:
return bool(japanese_imperial_era_year_reiwaheiseishowa_pattern.fullmatch(value))
# Example
print(validate_japanese_imperial_era_year_reiwaheiseishowa("令和6年1月15日")) # TrueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
令和6年1月15日 | 令和0年1月1日 |
R6年3月31日 | R100年1月1日 |
平成31年4月30日 | 明治30年1月1日 |
H31年1月1日 | 2024年1月15日 |
昭和64年1月7日 | — |
When to use this pattern
This pattern is drawn from the Localization > Date 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
Era year 1 of the new era and the last year of the previous era overlap — both are valid for the year of transition. 平成31年 and 令和元年 are both 2019.
Technical Notes
Era limits: 令和/Reiwa started May 1, 2019 (max year currently ~6). 平成/Heisei: 1-31 (1989-2019). 昭和/Showa: 1-64 (1926-1989). Meiji and Taisho are pre-1926 and excluded here. Convert to Gregorian for storage.
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