Qaynaq LABS · TOOLS

REGEX · PYTHON

Regex explained

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

Matches a string that must contain at least one lowercase letter, one uppercase letter, one digit, and be at least 8 characters long, using only specified characters.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: start of string Starts matching from the beginning
(?=.*[a-z]) Positive lookahead: ensures at least one lowercase letter exists anywhere in the string abc123ABC
(?=.*[A-Z]) Positive lookahead: ensures at least one uppercase letter exists anywhere in the string abc123ABC
(?=.*\d) Positive lookahead: ensures at least one digit exists anywhere in the string abc123ABC
[A-Za-z\d@$!%*?&]{8,} Character class matching any of the specified characters, repeated 8 or more times aA1@$!%b
$ Anchor: end of string Ensures no extra characters follow

Matches

  • Password1
  • Secure@123
  • Abcdefg1
  • A1b2C3d4
  • Test@1234

Does not match

  • password
  • PASSWORD1
  • 12345678
  • Pass word1
  • Pass@123
  • short1A
  • Pass@123$

Common pitfalls

  • Forgetting that lookaheads are zero-width assertions and do not consume characters.
  • Assuming the pattern allows spaces or other characters not in the character class.
  • Not realizing that the lookaheads check the entire string, not just the start.
  • Overlooking that the character class does not include all special characters by default.

Performance notes

The pattern is efficient for typical use cases but may cause catastrophic backtracking if the input string is very long and does not match early. The anchors (^ and $) help limit the scope. Consider pre-filtering input length before applying this regex.

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 there three lookaheads?

Each lookahead enforces a separate requirement: at least one lowercase letter, one uppercase letter, and one digit. They are combined to ensure all conditions are met simultaneously.

Can this pattern be simplified?

Not without losing the individual requirements. Alternative approaches like using multiple regexes or programmatic checks may be more readable but less concise.

Does this pattern allow Unicode characters?

No, it only matches ASCII letters, digits, and the specified special characters. For Unicode support, the character classes would need to be adjusted.

Why is the character class [A-Za-z\d@$!%*?&] used?

It restricts the allowed characters to letters (upper/lowercase), digits, and a specific set of special characters, ensuring the password meets complexity requirements.