I need a regex which can satisfy both SSN and Tax ID. It can take spaces or even hyphens.
Here are valid formats:
- 12-3456789
- 123456789
- 123 45 6789
- 123-45-6789
Can someone help?
I need a regex which can satisfy both SSN and Tax ID. It can take spaces or even hyphens.
Here are valid formats:
Can someone help?
You can use something like this: \d{2,3}[- ]?\d{2}[- ]?\d{4,5}
Test it out here: https://regex101.com/r/TJa5fE/2
My interpretation of the "valid" strings boils down to two distinct patterns which can use either -
, a whitespace character, or nothing at all as a separator. This pattern ensures that when a separator is used more than once, it matches across both instances by using the \k{}
token:
^\d{2}[-\s]?\d{7}$|\d{3}(?<sep>[-\s])\d{2}\k<sep>\d{4}
Be mindful that \k
is only supported in select RegExp engines (linked answer is slightly outdated, but still helpful starting point for research). I'll assume you're using a supported engine, as you haven't mentioned specifically what language/engine you're working with.