Regex to check for a repeating digit

I have a Java application in which users must provide a PIN for login. When creating a PIN code, there are only 3 requirements:

  • Must be 6 digits:

    \\d{6} 
  • There should not be 4 or more consecutive numbers:

     \\d*(0123|1234|2345|3456|4567|5678|6789)\\d* 
  • It is not necessary to have a number repeated 3 or more times (for example, 000957 or 623334 or 514888): This is where I got stuck ...

I tried:

 \\d*(\\d)\\1{3}\\d* 

but I believe that \1 looks at the initial match \d* not at the second match (\d) .


Answer used: I updated to use:
 \\d{6} (0123|1234|2345|3456|4567|5678|6789|9876|8765|7654|6543|5432|4321|3210) \\d*?(\\d)\\1{2,}\\d* 

To satisfy the originally stated requirements, plus a few that I did not think about! thanks for the help

+4
source share
5 answers

Your regex is slightly off, as the first \ d will match the first number. After that, you want 2 more matches.

 \\d*(\\d)\\1{2}\\d* 

gotta do the trick.

Quick edit: If you want to combine 2 or more numbers in a sequence, just add a comma to your account without specifying the maximum number:

 \\d*(\\d)\\1{2,}\\d* 

Or at least it works in Perl. Let us know how you are going.

+8
source

Do you want to block three repetitions of the following numbers or more than three numbers in total (for example, in "112213")?

If the latter matters, Regex may not be the best solution:

 public static boolean validate(String pin){ if (pin == null || pin.length() != 6) return false; int[] count = new int[10]; for (int i = 0; i < pin.length(); i++) { char c = pin.charAt(i); if(!Character.isDigit(c)) return false; if (++count[c - '0'] > 3) return false; } return true; } 
+4
source

I'd:

  • Check length == 6
  • Check \ d +
  • Frequency of counting each digit:

 int[] f = new int[10]; int pow10 = 1; int npow10 = 10; int nmod = 0, nmod2 = n % 10; while(i < 6) do int iDigit = (nmod2 - nmod)/pow10 if(++f[iDigit] > 2) return false; pow10 = npow10; npow10 *= 10; nmod = nmod2; nmod2 = n % npow10; i++; end return true; 

code>

+1
source

It looks like you are making three separate regular expressions, presumably denying the result of the second and third. In this case, this should do the trick:

 pinString.matches("^\\d{6}$") !pinString.matches("^.*?(?:0123|1234|2345|3456|4567|5678|6789).*$") !pinString.matches("^.*?(\\d)\\1{2}.*$") 

With the matches() method, you really don't need anchors ( ^ and $ ), but they don't hurt, and they make your intentions more obvious. In addition, the first regular expression ensures that all six characters are numbers, so it’s safe to use . instead of \\d as a space placeholder in the other two.

+1
source

Well your examples and your text do not match. You say that it cannot be repeated more than three times. 000 is a repeat of exactly 3 times, so it should be good, but 0000 is a repeat of 4, which is more than 3, so it should not. To match your textual requirement, your regular expression should be

 .*(\d)\1{3}.* 

What should be in the Java string

 ".*(\\d)\\1{3}.*" 

This is similar to what you have, so perhaps you just did not understand the wording.

Note. I like this to test my regex in Java: http://www.regexplanet.com/simple/index.html

0
source

Source: https://habr.com/ru/post/1389524/


All Articles