Regex to Match a US Social Security Number
Copy the pattern, paste your text below, and find every SSN-shaped value — useful for redaction. Below the tester: the format rules and which invalid ranges you may want to exclude.
What this pattern matches
This expression matches a US Social Security Number written as three digits, two digits, and four digits joined by hyphens — 123-45-6789. It is most often used to find and redact SSNs in documents and logs.
How it works, part by part
\b — a word boundary so it does not fire inside a longer digit run.
\d{3}-\d{2}-\d{4} — the area, group, and serial numbers separated by hyphens.
\b — a closing word boundary.
Excluding invalid ranges
The Social Security Administration never issues numbers with an area of 000, 666, or 900–999, nor a group or serial of all zeros. A stricter pattern such as (?!000|666|9\d\d)\d{3}-(?!00)\d{2}-(?!0000)\d{4} rejects those. For redaction, the simpler version above catches more and is usually what you want.
Use it in your code
import re
text = "SSN on file: 123-45-6789. Phone 555-12-3456 is not one. Also 987-65-4320."
pattern = r'''\b\d{3}-\d{2}-\d{4}\b'''
for m in re.finditer(pattern, text):
print(m.group())
const text = "SSN on file: 123-45-6789. Phone 555-12-3456 is not one. Also 987-65-4320.";
const re = new RegExp(String.raw`\b\d{3}-\d{2}-\d{4}\b`, "g");
console.log(text.match(re));
FAQ
Does it validate that the SSN is real?
No. It only checks the shape. Real validation requires the SSA rules for invalid area, group, and serial numbers.
How do I match SSNs without hyphens?
Use \b\d{9}\b, but be aware it will also match any nine-digit number such as a routing or account number.
Can I use it to redact SSNs?
Yes. Replace each match with a mask like XXX-XX-XXXX. Prefer the broader pattern so you do not miss any.