1

We want to check if number has repeat sequence for example - 111111 , 222222 etc. Below is the code :

String input = "222222";

public static String REGEX_REPEAT = "([\\d])\1{2}";

Pattern pattern = Pattern.compile(REGEX_REPEAT);
Matcher matcher = pattern.matcher(input.trim());
return matcher.matches();

But it always return false. I tried below regular expression also "([\\d])\\1{2}"

Any Help appreciated.

Thanks

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Praveen Mishra
  • 146
  • 1
  • 12

3 Answers3

1

Replace this

String REGEX_REPEAT = "([\\d])\1{2}";

with

String REGEX_REPEAT = "^(\\d)\\1+$";
Nikunj
  • 3,937
  • 19
  • 33
0

You can use following code

    import java.util.regex.*; 
class FindRepeatSequence {

    static boolean returnMatch(String input){
   String REGEX_REPEAT = "(\\w)\\1+";
   Pattern pattern = Pattern.compile(REGEX_REPEAT);
        Matcher matcher = pattern.matcher(input.trim());
    return matcher.matches();
    }

    public static void main(String[]args){
      String input = "222222";

     System.out.println("Print"+returnMatch(input));
    }
}
Sandeep dhiman
  • 1,863
  • 2
  • 17
  • 22
0

if you want to check the duplicate just use

   (\d)\1+\b 

and if you want to check duplicate with limit use

  //  (\d)\1{number_limit}\b   eg: 

     (\d)\1{3}\b