具有不重复最后 7 位数字的 NANP 电话号码的正则表达式

发布于 2024-12-02 12:30:32 字数 340 浏览 1 评论 0原文

我必须在 .NET 中验证 NANP 格式(不允许特殊字符)的 10 位(美国)电话号码,并检查以确保电话号码的最后 7 位不重复。到目前为止,我已经编写了以下正则表达式来验证 NANP 格式

^(?:[2-9][0-8][0-9])([2-9][0-9]{2}[0-9]{4})$

如何修改此正则表达式以也考虑不重复的最后 7 位数字?请注意,由于现有代码的限制,不能选择使用两个正则表达式。

编辑: 我必须检查所有 7 位数字中是否有连续的重复项。例如,2062222222 应被视为无效,而 2062221234 或 2062117777 应被视为有效。

谢谢

I have to validate a 10 digit (US) phone number in the NANP format (no special characters allowed) in .NET and also check to make sure the last 7 digits of the phone number are non-repeating. So far, I have written the following regex to validate the NANP format

^(?:[2-9][0-8][0-9])([2-9][0-9]{2}[0-9]{4})$

How do I modify this regex to also account for non-repeating last 7 digits? Please note that using two regexes is not an option due to constraints of existing code.

Edit:
I have to check for consecutive duplicates in all 7 digits. For e.g. 2062222222 should be considered invalid whereas 2062221234 or 2062117777 should be considered valid.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

心头的小情儿 2024-12-09 12:30:32

您是在谈论连续重复数字,还是所有七个数字都必须是唯一的?例如:

2342497553  // consecutive duplicates
2345816245  // non-consecutive duplicates
2345816249  // no duplicates

此正则表达式过滤掉连续的重复项:

^(?:[2-9][0-8][0-9])(?!.*(\d)\1)([2-9][0-9]{2}[0-9]{4})$

...而此正则表达式不允许任何重复数字:

^(?:[2-9][0-8][0-9])(?!.*(\d).*\1)([2-9][0-9]{2}[0-9]{4})$

在使用前三位数字后,先行尝试立即查找重复的字符( (?!.*(.)\1)) 或带有可选的中间字符 ((?!.*(.).*\1))。这是一个否定的前瞻,所以如果成功,整个匹配就会失败。


编辑:事实证明问题比我想象的要简单。要过滤掉像 2345555555 这样的数字,其中最后七位数字相同,请使用以下命令:

^(?:[2-9][0-8][0-9])(?!(\d)\1+$)([2-9][0-9]{2}[0-9]{4})$

包含结束锚点 ($) 非常重要,因为如果没有它,它将无法匹配有效数字,例如 2345555556。或者,您可以告诉它查找另外六个捕获的数字:(?!(\d)\1{6})

Are you talking about consecutive repeating digits, or do all seven digits have to be unique? For example:

2342497553  // consecutive duplicates
2345816245  // non-consecutive duplicates
2345816249  // no duplicates

This regex filters out consecutive duplicates:

^(?:[2-9][0-8][0-9])(?!.*(\d)\1)([2-9][0-9]{2}[0-9]{4})$

...while this one disallows any duplicate digits:

^(?:[2-9][0-8][0-9])(?!.*(\d).*\1)([2-9][0-9]{2}[0-9]{4})$

After the first three digits have been consumed, the lookahead tries to find a character that's repeated, either immediately ((?!.*(.)\1)) or with optional intervening characters ((?!.*(.).*\1)). And it's a negative lookahead, so if it succeeds, the overall match fails.


EDIT: It turns out the problem is simpler than I thought. To filter out numbers like 2345555555, where the last seven digits are identical, use this:

^(?:[2-9][0-8][0-9])(?!(\d)\1+$)([2-9][0-9]{2}[0-9]{4})$

It's important to include the end anchor ($), because without that it would fail to match valid numbers like 2345555556. Alternatively, you could tell it to look for exactly six more of the captured digit: (?!(\d)\1{6}).

木緿 2024-12-09 12:30:32

我很确定昨晚出现了非重复部分,并且普遍共识是正则表达式无法直接处理非重复,您必须放入大量难以管理的替代情况。我认为我还没有真正看到它被证明,但我很确定这是真的。归根结底,正则表达式没有记忆。我建议您使用正则表达式来验证格式并通过单独的算法运行它以检查重复。

I'm pretty sure the non-repeating portion of this came up last night and the general consensus was that regular expressions can't handle non-repetition directly, you'd have to put in an unmanageably large number of alternative cases. I don't think I've actually seen it proven, but I'm pretty sure it's true. It boils down to the fact that regular expressions have no memory. I suggest you use the regexp to validate the format and run it through a separate algorithm to check for repetition.

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