javaregexpasswords

password validation and UNICODE


How to validate regex for condition: Password must not contain any sequence of characters immediately followed by the same sequence of characters. I am having other conditions as well and am using

(?=.*(..+)\\1)

to validate for immediate sequence repeat. And it is failing. This piece of code returns "true" for 3rd and 4th strings passed; I need it to return false. Please help.

String s2[] = {"1newAb", "newAB1", "1234567AaAa", "123456ab3434", "love", "love1"}; 
    boolean b3;
    for(int i=0; i<s2.length; i++){
        b3 = s2[i].matches("^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*(..+)\\1).{5,12}$");
        System.out.println("value" + b3);
    }

Solution

  • You can try with negative look-ahead (?!.*(.{2,})\\1).

    For those who are wondering what \\1 is: it represents match from group 1, which in our case is match from (.{2,})