How to negate the whole regex?

Use negative lookaround: (?!pattern) Positive lookarounds can be used to assert that a pattern matches. Negative lookarounds is the opposite: it’s used to assert that a pattern DOES NOT match. Some flavor supports assertions; some puts limitations on lookbehind, etc. Links to regular-expressions.info Lookahead and Lookbehind Zero-Width Assertions Flavor comparison See also How do I … Read more

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 … Read more