Chinese Citizen ID Number (Shenfenzheng) Regex for Go
/^[1-8][0-9]{5}((?:19|20)[0-9]{2})(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])[0-9]{3}[0-9X]$/iWhat this pattern does
This page provides a comprehensive, battle-tested regular expression for matching chinese citizen id number (shenfenzheng), ported and verified for Go. Identity and credential patterns need both correctness and safety, since they're frequent targets for adversarial input. 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
// Chinese Citizen ID Number (Shenfenzheng)
// ReDoS-safe | RegexVault — Identity & PII > National Identity Numbers
package validation
import "regexp"
var chineseCitizenIdNumberShenfenzhengRe = regexp.MustCompile(`^[1-8][0-9]{5}((?:19|20)[0-9]{2})(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])[0-9]{3}[0-9X]$`)
func ValidateChineseCitizenIdNumberShenfenzheng(s string) bool {
return chineseCitizenIdNumberShenfenzhengRe.MatchString(s)
}
// Example
// fmt.Println(ValidateChineseCitizenIdNumberShenfenzheng("110101199003077515")) // trueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
110101199003077515 | 01010119900307771X |
440301200001010002 | 110101199013077515 |
31010519900307771X | 1101011990030777151 |
When to use this pattern
This pattern is drawn from the Identity & PII > National Identity Numbers 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
The sequence number (positions 15-17) encodes gender: odd=male, even=female. Region codes starting with 9 are used for special purposes (overseas Chinese, some SAR contexts). X as check character is uppercase only in official use.
Technical Notes
Structure: 6-digit region code (first digit 1-8) + 8-digit birthdate (YYYYMMDD) + 3-digit sequence + 1 check character. Check character X represents 10. Region code first digit 1-8 corresponds to Chinese provinces (9 not currently used for mainland). Checksum uses ISO 7064 MOD 11-2.
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