The java regular expression verification password must be a combination of more than 8 digits containing uppercase and lowercase letters, numbers, and special symbols

created at 12-24-2021 views: 7

Demand:

The password must be a combination of more than 8 characters containing uppercase letters, lowercase letters, numbers, and special symbols (not letters, numbers, underscores, Chinese characters)

Scheme:

Use regular expressions to verify

Ideas:

Exclusion

  1. Excluding 1 combination, 2 combinations, and 3 combinations among uppercase letters, lowercase letters, numbers, and special symbols, then only 4 combinations are left

  2. The expression is: ^(?![A-Za-z0-9]+$)(?![a-z0-9\\W]+$)(?![A-Za-z\\ W]+$)(?![A-Z0-9\\W]+$)[a-zA-Z0-9\\W]{8,}$

  3. Split interpretation: (2)-(6) uses regular functions such as zero-width assertion and look-around

  4. ^ matches the beginning

  5. (?![A-Za-z0-9]+$) matches the position that is not all (uppercase letters or lowercase letters or numbers), excluding 1 type of (uppercase letters, lowercase letters, numbers) 2 kinds of 3 kinds of combinations
  6. (?![a-z0-9\\W]+$) Same thing, excluding (lowercase letters, numbers, special symbols) 1, 2, 3 combinations
  7. (?![A-Za-z\\W]+$) Same thing, excluding (uppercase letters, lowercase letters, special symbols) 1 type, 2 types and 3 combinations
  8. (?![A-Z0-9\\W]+$) Same thing, excluding 1 type, 2 types and 3 types of combinations (capital letters, arrays, special symbols)
  9. [a-zA-Z0-9\\W] matching (lowercase letters or uppercase letters or numbers or special symbols) Because the above combinations are excluded, only 4 combinations are left
  10. {8,} 8 digits or more
  11. $ matches the end of the string

code

public class RegexTest {
    public static final String PW_PATTERN = "^(?![A-Za-z0-9]+$)(?![a-z0-9\\W]+$)(?![A-Za-z\\W]+$)(?![A-Z0-9\\W]+$)[a-zA-Z0-9\\W]{8,}$";

    public static void main(String[] args) {
        String pw1 = "ABCDEFGHIG";
        String pw2 = "abcdefghig";
        String pw3 = "0123456789";
        String pw4 = "!@#$%^&*()";
        String pw5 = "ABCDEabcde";
        String pw6 = "ABCDE01234";
        String pw7 = "ABCDE!@#$%";
        String pw8 = "abcde01234";
        String pw9 = "abcde!@#$%";
        String pw10 = "01234!@#$%";
        String pw11 = "abcde01234!@#$%";
        String pw12 = "ABCDE01234!@#$%";
        String pw13 = "ABCDEabcde!@#$%";
        String pw14 = "ABCDEabcde01234";
        String pw15 = "Aa0!";
        //Meet the required password
        String pw16="ABCabc012!@#";

        System.out.println(pw1.matches(PW_PATTERN));
        System.out.println(pw2.matches(PW_PATTERN));
        System.out.println(pw3.matches(PW_PATTERN));
        System.out.println(pw4.matches(PW_PATTERN));
        System.out.println(pw5.matches(PW_PATTERN));
        System.out.println(pw6.matches(PW_PATTERN));
        System.out.println(pw7.matches(PW_PATTERN));
        System.out.println(pw8.matches(PW_PATTERN));
        System.out.println(pw9.matches(PW_PATTERN));
        System.out.println(pw10.matches(PW_PATTERN));
        System.out.println(pw11.matches(PW_PATTERN));
        System.out.println(pw12.matches(PW_PATTERN));
        System.out.println(pw13.matches(PW_PATTERN));
        System.out.println(pw14.matches(PW_PATTERN));
        System.out.println(pw15.matches(PW_PATTERN));
        System.out.println(pw16.matches(PW_PATTERN));
    }
}

The output results are all false except for the last line, that is, only the last password meets the requirements

created at:12-24-2021
edited at: 12-27-2021: