Regular expression password - a lowercase letter cannot be followed by a lowercase letter and vice versa

So the specification:

  • 10 to 15 characters
  • has at least 1 small letter
  • has at least 1 capital letter
  • cannot have 2 small or 2 large letters in a row (a small letter cannot follow a small letter, and a large letter cannot be accompanied by a capital letter)

I thought I could do this with 5-7 pairs of characters and 1 optional at the beginning, so this gives 10 to 15 characters, but these (and many other tests) return false every time. Any ideas on the approach or syntax wrong?

thank

// regex expression
    private static final Pattern PASSWORD_PATTERN = Pattern.compile("^([a-z0-9]?([A-Z0-9][a-z0-9]){5,7}) 
        | ([A-Z0-9]?([a-z0-9][A-Z0-9]){5,7})$");

    // testing
    public static void main(String args[]) {
        System.out.println(verifyPassword("123456789123"));
        System.out.println(verifyPassword("AaAaAaAaAaAa3"));
    }
     // function for checking
     static boolean verifyPassword(String password) {
        final Matcher matcher = PASSWORD_PATTERN.matcher(password);
        return matcher.matches();
    }
+4
source share
2 answers

.matches(), ,

"(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])(?!.*(?:[A-Z]{2}|[a-z]{2}))\\p{Alnum}{10,15}"

  • ^ - , .matches() -
  • (?=[^A-Z]*[A-Z]) - ASCII 0+, ASCII
  • (?=[^A-Z]*[A-Z]) - ASCII 0+, ASCII
  • (?!.*(?:[A-Z]{2}|[a-z]{2})) - ([A-Z]{2}) ([A-Z]{2}) 0+, (.*)
  • \\p{Alnum}{10,15} - 10 15 ASCII
  • $ - .

. Java-:

List<String> strs = Arrays.asList("aaaa", "zzzzzzz", "AaAaAaAaAaAa3", "123456789123");
String pat = "(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])(?!.*(?:[A-Z]{2}|[a-z]{2}))\\p{Alnum}{10,15}";
for (String str : strs)
    System.out.println(str + ": " + str.matches(pat));

:

aaaa: false
zzzzzzz: false
AaAaAaAaAaAa3: true
123456789123: false
+1

:

^(?=.*[A-Z])(?=.*[a-z])(?!.*[A-Z][A-Z])(?!.*[a-z][a-z]).{10,15}$

+3

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


All Articles