I have a Java application where users must specify a PIN to log in. When creating the PIN, there are only 3 requirements:
-
Must be 6 digits:
\\d{6} -
Must not have 4 or more sequential numbers:
\\d*(0123|1234|2345|3456|4567|5678|6789)\\d* - Must not have a digit repeating 3 or more times (such as 000957 or 623334 or 514888):
This is where I’m stuck…
I have tried:
\\d*(\\d)\\1{3}\\d*
but I believe the \1 is looking at the initial match to the \d* not the second match of (\d).
Answer used:
I have updated to using:
\\d{6}
(0123|1234|2345|3456|4567|5678|6789|9876|8765|7654|6543|5432|4321|3210)
\\d*?(\\d)\\1{2,}\\d*
To satisfy the initially stated requirements plus a few I hadn’t thought of! Thanks for all the help
Your regex is slightly off, since the first \d will match the first number. You only want to match 2 more after that.
should do the trick.
Quick edit:
If you want to match 2 or more numbers in sequence, just add a comma to your count, without specifying a maximum number:
Or at least, this works in Perl. Let us know how you go.