SSN和电话号码的正则表达式 [英] Regular expression for SSN and phone number
问题描述
该字符串不应包含SSN或电话号码。下面的正则表达式不起作用,它只接受xxxxxxxxx格式。
The string should not contain SSN or phone number. The regex below does not work, it accepts only xxxxxxxxx format.
不应包含 xxx-xx-xxxx
或 xxx-xxx-xxx
或 xxxxxxxxx
。
regex =^((?!\\\\ [9] $)|(?!(\\\\ {3} - ?\\\\ {2} - ?\ \d {4} $)| $)$((\\d {3} - - \\d {3} \\d {3}?!??);
推荐答案
您可以尝试:
^(?!((\\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})$)$
我们可以读到:后面没有xxxxxxxxx或后面没有xxx-xx-xxxx或后面没有xxx-x xx-xxx
(在我的顶部版本中,我将其改为:不是(xxxxxxxxx或xxx-xx-xxxx或xxx-xxx-xxx)
。)。
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.
你的连字符不应该跟着?,我不这么认为。使它们成为可选项,意味着您还匹配 xxx-xxxxxx
和 xxx-xx-x-xxx
。如果你打算将它们添加回来,或者大大简化你的正则表达式,那么:
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}$).*
另一个问题是 \\d [9] $
应该是 \\\ \\ n {9} $
这篇关于SSN和电话号码的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!