vendredi 16 septembre 2022

How to generate 4 digit number as per regex

how to generate a 4-digit random pin which should be valid as per regex in java

For Example :

Regex is : ^(?!(.)\1{3})(?!19|20)(?!0123|1234|2345|3456|4567|5678|6789|7890|0987|9876|8765|7654|6543|5432|4321|3210)\d{4}$

String id = String.format("%04d", random.nextInt(10000));

using random generator it can generate any number which can violate the PIN rule

Like these should not be generated: 0123, 7890, 0987

Is there any way to generate PIN which should be valid as per regex ?




Aucun commentaire:

Enregistrer un commentaire