Identity
US Social Security Number (SSN) Regex
A US Social Security number is written as three digits, two digits and four digits separated by dashes. The Social Security Administration never issues area numbers 000, 666 or anything in the 900s, never uses group 00 and never uses serial 0000, and this pattern rejects all of those through negative lookaheads. That makes it useful for payroll forms, tax document intake and data loss prevention scans of structured fields. The limit is that it cannot confirm a number was actually assigned to anyone, and it requires the dashed format.
Pattern (JavaScript)
/^(?!000|666|9[0-9]{2})[0-9]{3}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$/Starts at the start of the line/string, then a position NOT followed by (the text "000" OR the text "666" OR the character "9", then a digit, exactly 2 times), then a digit, exactly 3 times, then the character "-", then a position NOT followed by (the text "00"), then a digit, exactly 2 times, then the character "-", then a position NOT followed by (the text "0000"), then a digit, exactly 4 times, then the end of the line/string.
Limit: Checks the shape only; it cannot tell whether a document is real or valid.
Examples
Matches
- ✓ 123-45-6789
- ✓ 234-56-7890
- ✓ 555-12-3456
Does not match
- ✗ 000-12-3456
- ✗ 666-12-3456
- ✗ 123-00-6789
- ✗ 123456789
Try it
0 matches (JavaScript engine)
123-45-6789
234-56-7890
555-12-3456
000-12-3456
666-12-3456
123-00-6789
123456789Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?!000|666|9[0-9]{2})[0-9]{3}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$/g;
const text = "123-45-6789";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?!000|666|9[0-9]{2})[0-9]{3}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$')
text = '123-45-6789'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(?!000|666|9[0-9]{2})[0-9]{3}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$/';
$text = '123-45-6789';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?!000|666|9[0-9]{2})[0-9]{3}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$");
String text = "123-45-6789";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
ℹ Go's RE2 engine has no lookaheads, so this checks the dashed shape only. Reject 000, 666, 9xx areas, group 00 and serial 0000 in Go code after matching.
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[0-9]{3}-[0-9]{2}-[0-9]{4}$`)
text := "123-45-6789"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?!000|666|9[0-9]{2})[0-9]{3}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$");
var text = "123-45-6789";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
ℹ The regex crate has no lookaheads, so this checks the dashed shape only. Reject the never-issued area, group and serial values in Rust code after matching.
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^[0-9]{3}-[0-9]{2}-[0-9]{4}$").unwrap();
let text = "123-45-6789";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?!000|666|9[0-9]{2})[0-9]{3}-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$/
text = '123-45-6789'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why does the pattern reject numbers starting with 9?
- Area numbers from 900 to 999 are not issued as Social Security numbers; that range is used for Individual Taxpayer Identification Numbers. Rejecting them keeps ITINs from being accepted in an SSN field.
- How can I accept SSNs typed without dashes?
- Make each dash optional by writing -? in its place, or strip non-digits from the input and test a nine-digit version. Keep the lookaheads so the never-issued values stay blocked.
- Is this pattern enough to protect sensitive data in logs?
- It helps detect well-formed SSNs, but data loss prevention tools usually also look for undashed and space-separated variants plus nearby keywords. Treat this as one signal among several.