正则&电话号码

发布于 2025-01-31 23:23:05 字数 281 浏览 3 评论 0原文

因此,我一直在努力创建以下格式验证电话号码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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万劫不复 2025-02-07 23:23:05

如果在您运行的任何版本中都支持负look-head,这应该有效:

(^(?!123-456-7890)[0-9]{3}-[0-9]{3}-[0-9]{4}$)

它与您使用的表达式相同,只是在开始时添加了负面的外观。

(?!123-456-7890)检查前面的字符序列是否与模式'123-456-7890'。
只有在满足这种情况时,将考虑其余模式。

在使用Look-Behind Expression (?<!123-456-7890)匹配模式之后,可以通过检查来完成同样的操作,

(^[0-9]{3}-[0-9]{3}-[0-9]{4}(?<!123-456-7890)$)

以防它更快或更实用你。

https://regex101.com/r/goyogf/1

If negative look-ahead is supported in whatever version you're running, this should work:

(^(?!123-456-7890)[0-9]{3}-[0-9]{3}-[0-9]{4}$)

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)

(^[0-9]{3}-[0-9]{3}-[0-9]{4}(?<!123-456-7890)$)

in case that's faster or more practical for you.

https://regex101.com/r/GOyOgf/1

乖乖公主 2025-02-07 23:23:05

为了简单起见,我会将它们分解为2个言论,您可以为“不匹配”使用交替:

        var regexToMatch = new Regex("(^[0-9]{3}-[0-9]{3}-[0-9]{4}$)");
        var regexToNotMatch = new Regex("(123-456-7890)|(098-765-4321)");
        
        var testString = "123-456-7890";
        
        if(regexToMatch.IsMatch(testString) && !regexToNotMatch.IsMatch(testString))
        {
            Console.WriteLine("Valid!");    
        }
        else
        {
            Console.WriteLine("Not Valid!");
        }

这是它的工作中的小提琴。

I would break them into 2 regexes for simplicity and you can use an alternation for the not matches, like this:

        var regexToMatch = new Regex("(^[0-9]{3}-[0-9]{3}-[0-9]{4}$)");
        var regexToNotMatch = new Regex("(123-456-7890)|(098-765-4321)");
        
        var testString = "123-456-7890";
        
        if(regexToMatch.IsMatch(testString) && !regexToNotMatch.IsMatch(testString))
        {
            Console.WriteLine("Valid!");    
        }
        else
        {
            Console.WriteLine("Not Valid!");
        }

And here's a working fiddle of it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文