简体   繁体   English

正则表达式匹配并非由所有相同号码组成的电话号码

[英]Regex to match a phone number that is not composed of all the same number

I need a regex that will match phone numbers that are not all composed of the same number.我需要一个正则表达式来匹配并非全部由相同号码组成的电话号码。 I'm talking about a 10 digit phone number that looks like this (123)123-1234.我说的是一个 10 位数的电话号码,看起来像这样 (123)123-1234。 I've seen patterns that will match phones that are all the same, but I'm trying to match the opposite.我已经看到了将匹配都是一样的手机模式,但我想匹配相反。

I've come up with this which is oh so close, but not quite there.我想出了这个,非常接近,但还没有完全到位。

^\\((\\d)(?!\\1{2})\\d{2}\\)(?!\\1{3})\\d{3}-(?!\\1{4})\\d{4}$

The only place this fails is when the area code is all the same number, everything else seems to work great.唯一失败的地方是当区号都是相同的数字时,其他一切似乎都很好。 So it will fail on something like this (888)123-1234, but will pass on (886)123-1234所以它会在这样的事情上失败(888)123-1234,但会通过(886)123-1234

How do I get it to accept that last hold out?我如何让它接受最后的坚持?

I've seen similar questions我看过类似的问题

how to Validate a Phone number so that it should not allow all same numerics like 99999999999 or 11111111111 in java 如何验证电话号码,以便它不允许在java中使用所有相同的数字,例如99999999999或11111111111

but this one doesn't account for the () and -, also it's matching the opposite of what I want.但是这个没有考虑到 () 和 -,它也与我想要的相反。

and

Regex to block a phone number that contains same digit more than 4 times successively? 正则表达式阻止包含相同数字的电话号码连续超过 4 次?

this kind of looks promising, but it doesn't account for the () and -.这种看起来很有希望,但它没有考虑到 () 和 -。

You can use this negative lookahead regex:您可以使用此否定前瞻正则表达式:

^\((\d)(?!\1{2}\)\1{3}-\1{4}$)\d{2}\)\d{3}-\d{4}$

RegEx Demo正则表达式演示

Negative lookahead (?!\\1{2}\\)\\1{3}-\\1{4}$) will only fail the match if same digit is repeated all over from start to end.负前瞻(?!\\1{2}\\)\\1{3}-\\1{4}$)只会在从头到尾重复相同的数字时匹配失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM