Regex to Match Social Security Numbers
Validate US Social Security Numbers (SSN) format with this regex pattern. Checks the XXX-XX-XXXX format with invalid range exclusions. Free tool.
Regular Expression
/^(?!000|666|9\d{2})\d{3}-(?!00)\d{2}-(?!0000)\d{4}$/
Token Breakdown
| Token | Description |
|---|---|
| ^ | Anchors at the start of the string (or line in multiline mode) |
| (?! | Start of negative lookahead assertion |
| 0 | Matches the literal character '0' |
| 0 | Matches the literal character '0' |
| 0 | Matches the literal character '0' |
| | | Alternation — matches the expression before OR after the pipe |
| 6 | Matches the literal character '6' |
| 6 | Matches the literal character '6' |
| 6 | Matches the literal character '6' |
| | | Alternation — matches the expression before OR after the pipe |
| 9 | Matches the literal character '9' |
| \d | Matches any digit (0-9) |
| {2} | Matches exactly 2 times |
| ) | End of group |
| \d | Matches any digit (0-9) |
| {3} | Matches exactly 3 times |
| - | Matches the literal character '-' |
| (?! | Start of negative lookahead assertion |
| 0 | Matches the literal character '0' |
| 0 | Matches the literal character '0' |
| ) | End of group |
| \d | Matches any digit (0-9) |
| {2} | Matches exactly 2 times |
| - | Matches the literal character '-' |
| (?! | Start of negative lookahead assertion |
| 0 | Matches the literal character '0' |
| 0 | Matches the literal character '0' |
| 0 | Matches the literal character '0' |
| 0 | Matches the literal character '0' |
| ) | End of group |
| \d | Matches any digit (0-9) |
| {4} | Matches exactly 4 times |
| $ | Anchors at the end of the string (or line in multiline mode) |
Detailed Explanation
This regex validates US Social Security Numbers (SSN) in the standard XXX-XX-XXXX format with exclusions for known invalid ranges. Here is the token-by-token breakdown:
^ — Anchors the match at the start of the string.
(?!000|666|9\d{2}) — A negative lookahead that rejects invalid area numbers. The SSA has never issued SSNs starting with 000 or 666, and numbers starting with 9XX were reserved for the Individual Taxpayer Identification Number (ITIN) program. This lookahead checks ahead without consuming characters.
\d{3} — Matches exactly three digits for the area number (first three digits).
- — Matches a literal hyphen separator.
(?!00) — A negative lookahead rejecting group numbers of 00, which are not valid in real SSNs.
\d{2} — Matches exactly two digits for the group number (middle two digits).
- — Matches a literal hyphen separator.
(?!0000) — A negative lookahead rejecting serial numbers of 0000, which are not valid.
\d{4} — Matches exactly four digits for the serial number (last four digits).
$ — Anchors the match at the end of the string.
This pattern enforces the correct format and excludes ranges that the Social Security Administration has declared invalid. Note that this is a format validation only; it does not verify that the SSN has actually been issued. Handle SSN data with appropriate security measures, as it is sensitive personally identifiable information.
Example Test Strings
| Input | Expected |
|---|---|
| 123-45-6789 | Match |
| 000-12-3456 | No Match |
| 666-12-3456 | No Match |
| 900-12-3456 | No Match |
| 555-55-5555 | Match |
Try It — Interactive Tester
51 charsFlags: noneMatches: 0Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match US Phone Numbers
/^(?:\+1\s?)?(?:\(?\d{3}\)?[\s.-]?)?\d{3}[\s.-]?\d{4}$/
Regex to Match Credit Card Numbers
/^(?:4\d{3}|5[1-5]\d{2}|3[47]\d{2}|6(?:011|5\d{2}))[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{1,4}$/
Regex to Match US Passport Numbers
/^[A-Z]\d{8}$/
Regex to Match US Zip Codes
/^\d{5}(?:-\d{4})?$/