Can anyone explain this regexp password complexity to me?

\A(?=\S*?[AZ])(?=\S*?[az])(?=\S*?[0-9])\S{6,}\z 

(this comes from the JGSoft regular expression flavor).

Basically, the purpose of this regular expression is: β€œPassword complexity Tests if the input consists of 6 or more characters. The input must contain at least one uppercase letter, one lowercase letter and one number.

Thus, there are three groups that can be placed in any order (first capital letters, then non-capital letters, then numbers). \ A corresponds to the beginning of a line, and \ z corresponds to the end of a line. I got a little confused in (? = Part. This should be a positive look. I don't see how this regular expression allows you to enter password characters in any order.

+2
source share
2 answers

Sounds like you got the basic idea. Think of it this way:

  • Look from the beginning of the text. Although at the beginning:
    • Check there at least one letter in the upper case (just looking, do not advance).
    • Check at least one lowercase letter.
    • Check at least one digit there.
  • Match at least 6 characters other than whitesapce to the end.

Expectations are not captured - you stay in the same place (in this case, at the beginning), and check if you can match this forward - why the order does not matter.

Here is an example to enter 1@34Ab .

  • \A - matches the beginning of a line. large.
  • (?=\S*?[AZ]) - \S*?[AZ] can match 1@34A , approval (?=) Succeeds.
  • (?=\S*?[AZ]) - may correspond to 1@34Ab .
  • (?=\S*?[0-9]) - may correspond to 1 .
  • \S{6,} - Corresponds to 1 @ 34Ab, up to the end ( \z ).
+4
source

Lookahead means that I check if there is a template without changing the location, therefore

  • check if from the beginning of the line there is zero or more none space followed by upper case last.

  • then it again checks the beginning of the line if there is zero or more no spaces and then lower case.

  • then it again checks the beginning of the line, if there is zero or more there is no space then numeric.

  • Finally, it checks to see if the string is 6 or more space characters.

first 3 tests (uppercase, lowercase, numeric) lookahead - the check starts from the beginning of the line without changing the location, only the last test (6 or more characters) changes the location. lookahead is his name, just look and see what happens without movement, so the first 3 tests are performed from the beginning of the line, so the order is not important as long as all 3 are positive.

+1
source

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


All Articles