For half an hour I have been fighting over the expression.

The point is to exclude phone numbers containing more than 4 consecutive inclusions of the same numbers, ie:

893700000000 893777777777

and so on. All this business should work in Java, I try matches ("7 {4,}") for an example 893777777777, on an output I receive false.

    3 answers 3

    String number = "89371477777"; Pattern p = Pattern.compile("1{4,}|2{4,}|3{4,},|4{4,}|5{4,}|6{4,}|7{4,}"); Matcher m = p.matcher(number); if (m.find()) { System.out.println("Найдено"); } 
    • It would be better if you brought a piece of code - G1yyK
    • one
      @ G1yyK it will be 4 any numbers, not the same. - a_gura 2:07 pm
    • Oops, Thu immediately did not doper - G1yyK
    • 1 {4,} | 2 {4,} | 3 {4,} .... - G1yyK
    • String number = "89371477777"; Pattern.compile ("1 {4,} | 2 {4,} | 3 {4,}, | 4 {4,} | 5 {4,} | 6 {4,} | 7 {4,}"); Matcher m = p.matcher (number); boolean b = m.matches (); System.out.println (b); - notkeo

    It may be easier :

     (\d)\1{3} 

      There is a simpler option:

       Pattern p = Pattern.compile("(\\d{2})\\1"); if(p.matcher("89371477777").find()) { // .. } 
      • one
        Not really: ideone.com/9hCuxM - VladD
      • I did not think about such couples, thanks! - Pavel Azanov 2:44 pm