SSN Regex Python Validator

Search...

⌘K

SSN Regex Python Validator

Search...

⌘K


SSN Regex Python Validator

SSN Regex Python Validator

Validate US Social Security Numbers (SSNs) effortlessly with the SSN Regex Python Validator. This tool ensures the correct “AAA-GG-SSSS” structure using Python’s re module. Also explore Email Regex Python Validator, Phone Number Regex Python Validator, and Python Regex Tester for broader input validation.

bb:aa:dd:aa:55:55
Possible security issues
This regex appears to be safe.
Explanation
  • [A-Z]: uppercase letters
  • [a-z]: lowercase letters
  • [0-9]: digits
  • \.: a literal dot
  • +: one or more of the preceding
  • *: zero or more of the preceding
  • ?: optional (zero or one)
  • ^: start of string
  • $: end of string
Match information
Match 1: "bb:aa:dd:aa:55:55" at index 0
Test your APIs today!

Write in plain English — Qodex turns it into secure, ready-to-run tests.

Regular Expression - Documentation

What is SSN Regex?


A Social Security Number (SSN) in the U.S. follows the standard format:

XXX-XX-XXXX, where:

  • XXX: Area number (3 digits)

  • XX: Group number (2 digits)

  • XXXX: Serial number (4 digits)


This format ensures structured identification for tax and legal purposes. Using regex helps validate whether an input string follows this format strictly.


SSN Regex Pattern


The typical regex pattern to validate a properly formatted SSN is:

^\d{3}-\d{2}-\d{4}$


Breakdown:

  • ^\d{3} → Three digits at the start

  • - → First hyphen separator

  • \d{2} → Two digits

  • - → Second hyphen

  • \d{4}$ → Four digits at the end


This ensures strict format matching like 123-45-6789.


Python Example Code


import re

def is_valid_ssn(ssn):
    pattern = re.compile(r'^\d{3}-\d{2}-\d{4}$')
    return bool(pattern.fullmatch(ssn))

# Test Cases
print(is_valid_ssn("123-45-6789"))  # True
print(is_valid_ssn("123456789"))    # False
print(is_valid_ssn("12-345-6789"))  # False

Test regex variations with the Python Regex Tester.


Use Cases


  • User Identity Verification: Ensure accurate SSN input during user registration or onboarding.

  • Data Cleaning: Detect and correct improperly formatted SSNs in databases.

  • Security & Compliance: Verify SSN format before transmitting sensitive data.


You can combine this with:


Regex Metacharacters Used


  • ^ : Anchors the start of the string

  • \d : Matches any digit (0–9)

  • {n} : Matches exactly n digits

  • - : Matches literal hyphens in SSNs

  • $ : Anchors the end of the string


Pro Tips


  • Always anchor patterns with ^ and $ to avoid partial matches.

  • Preprocess user input to trim whitespaces before validation.

  • You can extend validation to exclude invalid combinations (like 000 or 666) using advanced regex or logic.

  • Avoid storing plain SSNs—encrypt them after validation.

  • Use Python Regex Tester to experiment before deploying.

  • Combine with Date Regex Python Validator if SSNs are part of a larger structured document.


Frequently asked questions

Can this regex validate both hyphenated and non-hyphenated SSNs?×
No, the current pattern (^\d{3}-\d{2}-\d{4}$) strictly matches the hyphenated format. To allow both formats, you’d need to modify the pattern to: ^\d{3}-?\d{2}-?\d{4}$.
Does this regex detect invalid or disallowed SSN combinations like 000 or 666?+
Is this pattern sufficient for use in government or financial systems?+
What if users enter leading/trailing spaces in the SSN?+
Can I use this pattern inside a larger data validation pipeline?+