Qaynaq LABS · TOOLS

REGEX · PYTHON

Regex explained

^(?=.*\[a-z\])(?=.*[A-Z\])(?=.*\d)(?=.*[@$!%*?&\])[A-Za-z\d@$!%*?&\]{8,}$

A regex that enforces a password policy requiring at least one lowercase letter, one uppercase letter, one digit, and one special character from a defined set, with a minimum length of 8 characters.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts the position at the start of the string Start of string
(?= Positive lookahead: asserts that the following pattern must match at the current position without consuming characters Lookahead assertion
.* Any character (except newline) zero or more times abc
\[ Escaped literal '[' character [
a-z Character class: any lowercase letter from a to z a
\] Escaped literal ']' character ]
) End of lookahead assertion End of assertion
(?= Positive lookahead: asserts that the following pattern must match at the current position without consuming characters Lookahead assertion
.* Any character (except newline) zero or more times abc
[ Start of character class Start of class
A-Z Character class: any uppercase letter from A to Z A
\ Escaped literal '\' character (required to escape ']' in character class) \
] End of character class End of class
) End of lookahead assertion End of assertion
(?= Positive lookahead: asserts that the following pattern must match at the current position without consuming characters Lookahead assertion
.* Any character (except newline) zero or more times abc
\d Digit character class: any digit 0-9 1
) End of lookahead assertion End of assertion
(?= Positive lookahead: asserts that the following pattern must match at the current position without consuming characters Lookahead assertion
.* Any character (except newline) zero or more times abc
[ Start of character class Start of class
@$!%*?& Character class: any of the specified special characters @
\ Escaped literal '\' character (required to escape ']' in character class) \
] End of character class End of class
) End of lookahead assertion End of assertion
[ Start of character class Start of class
A-Za-z\d@$!%*?& Character class: any uppercase letter, lowercase letter, digit, or specified special character A
] End of character class End of class
{8,} Quantifier: matches the preceding token 8 or more times 8 or more characters
$ Anchor: asserts the position at the end of the string End of string

Matches

  • Password1!
  • Abcdefg2@
  • ZxYwVuT9#

Does not match

  • password
  • PASSWORD
  • 12345678
  • Pass123
  • Pass word1!
  • Pass123$
  • Pass1234

Common pitfalls

  • Missing one of the required character types (e.g., no uppercase or no special character)
  • Using a character not in the allowed set (e.g., '^' or '=')
  • Length less than 8 characters
  • Escaping issues with ']' in character classes (e.g., writing [A-Z] instead of [A-Z\])

Performance notes

The pattern uses four lookaheads which each scan the entire string, leading to O(n) complexity per lookahead. While not catastrophic backtracking, it may be inefficient for very long strings. Anchoring at start (^) and end ($) ensures the entire string is evaluated. Consider optimizing by reducing lookaheads if possible.

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Why are the square brackets escaped in the lookaheads like \[a-z\]?

The square brackets are escaped to treat them as literal characters rather than as part of a character class. In regex, '[' and ']' have special meanings, so escaping them ensures they are matched literally.

Can I simplify the character class [A-Za-z\d@$!%*?&]?

Yes, you can simplify it to [A-Za-z0-9@$!%*?&] for better readability. The \d is equivalent to 0-9, and the character class remains the same.

Why does the pattern use lookaheads instead of direct matching?

Lookaheads are used to ensure that the string contains at least one of each required character type without consuming characters. This allows the main pattern to match the entire string while enforcing the presence of all required types.