Regular expression for SSN and phone number [closed]

对着背影说爱祢 提交于 2019-12-02 23:55:57

问题


The string should not contain SSN or phone number. The regex below does not work, it accepts only xxxxxxxxx format.

Should not contain xxx-xx-xxxx or xxx-xxx-xxx or xxxxxxxxx.

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


回答1:


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}$



来源:https://stackoverflow.com/questions/14103840/regular-expression-for-ssn-and-phone-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!