Pattern Breakdown
Understanding the regex pattern ^(?!000|666|9\d{2})\d{3}-(?!00)\d{2}-(?!0000)\d{4}$:
- ^ - Start of string
- $ - End of string
- \d - Any digit (0-9)
- ? - Optional (zero or one)
- (...) - Capturing group
- {n,m} - Quantifier (n to m times)
- | - Alternation (OR)
Examples
| Input | Result | Explanation |
|---|---|---|
123-45-6789 |
✓ Match | Valid SSN format |
000-45-6789 |
✗ No Match | Invalid area number |
666-45-6789 |
✗ No Match | Reserved area number |
Code Examples
JavaScript
JavaScript
const pattern = /^(?!000|666|9\d{2})\d{3}-(?!00)\d{2}-(?!0000)\d{4}$/;
const text = "123-45-6789";
// Test if string matches
if (pattern.test(text)) {
console.log("Valid!");
}
// Extract matches
const matches = text.match(pattern);
console.log(matches);
Python
Python
import re
pattern = r'^(?!000|666|9\d{2})\d{3}-(?!00)\d{2}-(?!0000)\d{4}$'
text = "123-45-6789"
# Test if string matches
if re.match(pattern, text):
print("Valid!")
# Find all matches
matches = re.findall(pattern, text)
print(matches)
PHP
PHP
<?php
$pattern = '/^(?!000|666|9\d{2})\d{3}-(?!00)\d{2}-(?!0000)\d{4}$/';
$text = "123-45-6789";
// Test if string matches
if (preg_match($pattern, $text)) {
echo "Valid!";
}
// Find all matches
preg_match_all($pattern, $text, $matches);
print_r($matches);
?>
Java
Java
import java.util.regex.*;
String pattern = "^(?!000|666|9\\d{2})\\d{3}-(?!00)\\d{2}-(?!0000)\\d{4}$";
String text = "123-45-6789";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);
if (m.matches()) {
System.out.println("Valid!");
}
Use Cases
Common applications: HR systems, government forms, identity verification
- Form validation and input sanitization
- Data extraction and parsing
- Search and replace operations
- API request/response validation
- Log file analysis
Frequently Asked Questions
What is the regex pattern for us social security number regex?
The regex pattern is: ^(?!000|666|9\d{2})\d{3}-(?!00)\d{2}-(?!0000)\d{4}$. Validate US SSN format XXX-XX-XXXX with Area, Group, and Serial number rules.
How do I use the us social security number regex pattern in JavaScript?
In JavaScript: const regex = new RegExp('^(?!000|666|9\\d{2})\\d{3}-(?!00)\\d{2}-(?!0000)\\d{4}$'); or const regex = /^(?!000|666|9\d{2})\d{3}-(?!00)\d{2}-(?!0000)\d{4}$/;
Does this pattern work in Python?
Yes, use: import re; pattern = re.compile(r'^(?!000|666|9\d{2})\d{3}-(?!00)\d{2}-(?!0000)\d{4}$') in Python. Most regex patterns are cross-language compatible.
What are common use cases for us social security number regex?
HR systems, government forms, identity verification
Related Regex Patterns
More Regex Tools
GetDevUtils offers a suite of developer tools including:
- Regex Tester - Test and debug regular expressions
- JSON Formatter - Format and validate JSON
- Base64 Encoder/Decoder - Encode and decode Base64
- URL Encoder/Decoder - Encode and decode URLs