This regex pattern matches American Social Security Numbers. It first uses \b to match word boundaries. Then it uses negative lookahead assertions to exclude invalid SSN patterns like ones starting with 000, 666, or in the 900-999 range. It then matches the standard SSN format of '###-##-####'.
\\b(?!000|666|9\\d{2})\\d{3}-(?!00)\\d{2}-(?!0000)\\d{4}\\b
Copia
import re
ssn_pattern = r'\b(?!000|666|9\d{2})\d{3}-(?!00)\d{2}-(?!0000)\d{4}\b'
ssn = '123-45-6789'
if re.match(ssn_pattern, ssn):
print('Valid SSN')
Copia