Meaning
Compiled regular expression that matches strings exactly matching the US Social Security Number format (XXX-XX-XXXX).
Primary Function
Compile a regex pattern for validating or extracting US Social Security Numbers.
Communicative Purpose
Validate or extract SSN strings in data validation, cleaning, or PII handling tasks.
Pattern
re.compile(r'^\d{3}-\d{2}-\d{4}$')
Core Structure
re.compile(...)
Função primária
Compile a regex pattern for validating or extracting US Social Security Numbers.
Propósito comunicativo
Validate or extract SSN strings in data validation, cleaning, or PII handling tasks.
Situações de gatilho
Validating user input for SSN fields, cleaning PII datasets, validating data imports, log parsing for SSN patterns.
Contextos
Data validation, data cleaning, PII handling, form validation, log parsing.
Padrão
re.compile(r'^\d{3}-\d{2}-\d{4}$')
Estrutura central
re.compile(...)
Slots de substituição
area_number: int (exactly 3 digits), group_number: int (exactly 2 digits), serial_number: int (exactly 4 digits)
Colocados típicos
- SSN
- social security number
- validate
- re.match
- re.search
- re.fullmatch
- input validation
- PII
Substituições comuns
- r'\d{3}-\d{2}-\d{4}' (without anchors)
- r'\d{9}' (condensed)
- r'\d{3}\s?\d{2}\s?\d{4}' (optional spaces)
Erros comuns
Omitting ^ and $ anchors leading to partial matches; using \d{9} ignoring hyphens; using \d{3}-\d{2}-\d{4} but allowing extra characters before/after; forgetting raw string r'...' in Python causing incorrect escape sequences.
Similar / contraste
US phone number regex r'^\d{3}-\d{3}-\d{4}$' (different grouping); US ZIP+4 regex r'^\d{5}(-\d{4})?$' (different length); Canadian SIN regex r'^\d{3}\s?\d{3}\s?\d{3}$' (different separator and grouping).
Interferências
Coming from JavaScript: forgetting to use raw string r'...' in Python causing incorrect escape interpretation; Coming from Java: forgetting to compile pattern with re.compile and using String.matches incorrectly; Coming from JavaScript: using \d without escaping backslash in string literal.
Família do chunk
- regex patterns
- validation patterns
- PII validation
- SSN validation
Nuance
Do not rely solely on this regex for SSN validity as it does not check prohibited ranges (e.g., all zeros, 666, or 900-999 series); Pre‑compiling with re.compile yields significant performance gains when matching many strings; The pattern strictly enforces hyphen separators and rejects spaces or other separators.
Efeito pragmático
Using a pre‑compiled SSN regex ensures fast, reliable format validation, preventing malformed SSNs from entering databases and reducing downstream validation errors.
Dica de memória
Think of the pattern as a telephone‑line format: three digits, a dash, two digits, another dash, then four digits — just like a phone number but with different grouping.
Nota
This regex validates format only; it does not verify that the number is a genuine issued SSN (e.g., not in the SSA's high‑group list).
Upgrade path
Consider adding SSN range validation (e.g., reject 000-##-####, 666-##-####, 900-999-##-####) or using a library that validates against the SSA's high‑group list.
Log in to save chunks.