Regular expression for SSN and phone number [closed]

You might try:

^(?!((\\d{9})|(\\d{3}-\\d{2}-\\d{4})|(\\d{3}-\\d{3}-\\d{3}))$).*

To explain, if we read the query you provided:

^((?!\\d[9]$)|(?!(\\d{3}-?\\d{2}-?\\d{4}$)|(?!(\\d{3}-?\\d{3}-?\\d{3})$)$

We could read that: is not followed by xxxxxxxxx OR is not followed by xxx-xx-xxxx OR is not followed by xxx-xxx-xxx (in my version at the top, I rephrased this to be: is not (xxxxxxxxx OR xxx-xx-xxxx OR xxx-xxx-xxx).).

Any string in the world is guaranteed to not match at least two of those, so the combination of them is always true, leaving you with a much simpler effective regex:

^$

?! is a zero-width assertion, so it consumes nothing. Even when you match what’s checked by the lookaheads, you aren’t consuming the input, and so you never reach $, that’s required outside the lookaheads. Simply adding a .* before the final $ fixes that.

Your hyphens shouldn’t be followed by ?, I don’t think. Making them optional, means you also match xxx-xxxxxx and xxx-xx-x-xxx. If that’s intended you can add them back, or simplify your regex considerably, to:

^(?!\\d{3}-?\\d{2}-?\\d-?\\d{3}$).*

The other problem is \\d[9]$ should be \\d{9}$

Leave a Comment