Regular Expressions with .net – U.S. Social Security Numbers
In the United States, a Social Security number (abbreviated as SSN) is a nine-digit number issued to U.S. citizens, permanent residents, and temporary (working) residents. U.S. social security numbers are three sets of digits separated by hyphens; the first set contains three digits, the second set contains two digits, and the third set contains four digits. Its primary purpose is to track individuals for taxation purposes. In recent years the SSN has become a de facto national identification number. A properly formatted US social security number. first three digits must be 001 – 772.
Regular Expression Pattern
^((?!000)([0-6]\d{2}|[0-7]{2}[0-2]))-((?!00)\d{2})-((?!0000)\d{4})$
A description of the regular expression:
[1]: A numbered capture group. [(?!000)([0-6]\d{2}|[0-7]{2}[0-2])]
(?!000)([0-6]\d{2}|[0-7]{2}[0-2])
Match if suffix is absent. [000]
[2]: A numbered capture group. [[0-6]\d{2}|[0-7]{2}[0-2]]
Select from 2 alternatives
[0-6]\d{2}
Any character in this class: [0-6]
Any digit, exactly 2 repetitions
[0-7]{2}[0-2]
Any character in this class: [0-7], exactly 2 repetitions
Any character in this class: [0-2]
-
[3]: A numbered capture group. [(?!00)\d{2}]
(?!00)\d{2}
Match if suffix is absent. [00]
Any digit, exactly 2 repetitions
-
[4]: A numbered capture group. [(?!0000)\d{4}]
(?!0000)\d{4}
Match if suffix is absent. [0000]
Any digit, exactly 4 repetitions




