Python (re)

US Social Security Number in PY

Match US Social Security Numbers in the canonical XXX-XX-XXXX format.

Try it in the PY tester →

Pattern

regexPY
\b\d{3}-\d{2}-\d{4}\b   (flags: g)

Python (re) code

pyPython
import re

pattern = re.compile(r"\b\d{3}-\d{2}-\d{4}\b")
input_text = "123-45-6789"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

How the pattern works

Three digits, hyphen, two digits, hyphen, four digits. Word boundaries prevent false matches inside longer digit runs. Note: this pattern does not validate SSN issuance rules.

Examples

Input

123-45-6789

Matches

  • 123-45-6789

Input

SSN: 987-65-4321 on file

Matches

  • 987-65-4321

Input

123456789

No match

Same pattern, other engines

← Back to US Social Security Number overview (all engines)