REGEXVAULTv2.0
Identity & PII/National Identity Numbers
Verified Safe

Chinese Citizen ID Number (Shenfenzheng) Regex for Python

/^[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]$/i

What this pattern does

This page provides a comprehensive, battle-tested regular expression for matching chinese citizen id number (shenfenzheng), ported and verified for Python. 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 Python project — whether you're validating in a Django view, a FastAPI endpoint, or a standalone data processing script.

Python Implementation

Python
# Chinese Citizen ID Number (Shenfenzheng)
# ReDoS-safe | RegexVault — Identity & PII > National Identity Numbers

import re

chinese_citizen_id_number_shenfenzheng_pattern = re.compile(r'^[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]$')

def validate_chinese_citizen_id_number_shenfenzheng(value: str) -> bool:
    return bool(chinese_citizen_id_number_shenfenzheng_pattern.fullmatch(value))

# Example
print(validate_chinese_citizen_id_number_shenfenzheng("110101199003077515"))  # True

Test Cases

Matches (Valid)
Rejects (Invalid)
11010119900307751501010119900307771X
440301200001010002110101199013077515
31010519900307771X1101011990030777151

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 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

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