正则&电话号码
因此,我一直在努力创建以下格式验证电话号码xxx-xxx-xxxx的正则验证,该号码迄今为止运作良好,但是现在我试图弄清楚如何删除输入特定数字的能力“ 123-456-7890”。
我当前的正则是
(^[0-9]{3}-[0-9]{3}-[0-9]{4}$)
我已经研究了几天,但是我似乎无法弄清楚要添加到我当前的表达式中,以便获得“使用格式的任何数字xxx-xxx-xxxx,但不是123-- 456-7890“
有人可以帮我吗?
太感谢了
So I have been working on creating a regex to validate phone numbers in the following format XXX-XXX-XXXX which has been working pretty well so far, but now I'm trying to figure out how to remove the ability to enter specific numbers like "123-456-7890".
My current regex is
(^[0-9]{3}-[0-9]{3}-[0-9]{4}$)
I've been looking into it for a few days, but I can't seem to figure out what to add to my current expression in order to get "any number with formats XXX-XXX-XXXX BUT NOT 123-456-7890"
Can anyone help me with this?
Thank you so much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果在您运行的任何版本中都支持负look-head,这应该有效:
它与您使用的表达式相同,只是在开始时添加了负面的外观。
(?!123-456-7890)
检查前面的字符序列是否与模式'123-456-7890'。只有在满足这种情况时,将考虑其余模式。
在使用Look-Behind Expression
(?<!123-456-7890)匹配模式之后,可以通过检查来完成同样的操作,
以防它更快或更实用你。
https://regex101.com/r/goyogf/1
If negative look-ahead is supported in whatever version you're running, this should work:
It's the same expression you used, just with a negative look-ahead added in the beginning.
(?!123-456-7890)
checks if the character sequence ahead doesn't match the pattern '123-456-7890'.Only if this condition is met, the rest of the pattern will be considered.
The same can be done by checking after matching the pattern using the look-behind expression
(?<!123-456-7890)
in case that's faster or more practical for you.
https://regex101.com/r/GOyOgf/1
为了简单起见,我会将它们分解为2个言论,您可以为“不匹配”使用交替:
这是它的工作中的小提琴。
I would break them into 2 regexes for simplicity and you can use an alternation for the not matches, like this:
And here's a working fiddle of it.