Japanese Imperial Era Year (Reiwa/Heisei/Showa) Regex for Go
/^(?:(令和|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 Go. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your Go project — whether you're validating in a Gin handler, a gRPC service, or a command-line tool.
Go Implementation
// Japanese Imperial Era Year (Reiwa/Heisei/Showa)
// ReDoS-safe | RegexVault — Localization > Date Formats
package validation
import "regexp"
var japaneseImperialEraYearReiwaheiseishowaRe = regexp.MustCompile(`^(?:(令和|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])日$`)
func ValidateJapaneseImperialEraYearReiwaheiseishowa(s string) bool {
return japaneseImperialEraYearReiwaheiseishowaRe.MatchString(s)
}
// Example
// fmt.Println(ValidateJapaneseImperialEraYearReiwaheiseishowa("令和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 Go developers because Go's RE2 engine is inherently safe from catastrophic backtracking, but this pattern has been additionally verified for correctness. 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