A signup form asks for a password that is "at least 8 characters and contains an uppercase letter, a lowercase letter, a digit and a symbol". Behind that one sentence sit five rules, and the regular expression that enforces them has to check all five against the same string. Written in one pass, it looks like alphabet soup and is miserable to debug. Built one rule at a time, it is mechanical.

This guide builds the pattern piece by piece with the free Regex Tester on Ovatly, tests passwords that pass and fail each rule, and shows how to work out which rule a rejected password broke. It also states the honest limit up front: this pattern checks what a password is made of, not whether it is hard to guess.

Contents

  • Quick answer
  • One rule per lookahead
  • The five pieces of the pattern
  • How to build and test the pattern, step by step
  • Worked example: four passwords against the full pattern
  • How to find which rule a password failed
  • What a composition regex cannot check
  • Common mistakes
  • FAQs
  • Conclusion

Quick answer

The full pattern is ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{8,}$. Read it as five checks: at least one lowercase letter, at least one uppercase letter, at least one digit, at least one symbol, and at least 8 characters, anchored so the whole string must qualify. Paste passwords into the Regex Tester, one per test, and a match means the password satisfies every rule. When one fails, test each lookahead separately to see which ingredient is missing.

One rule per lookahead

A lookahead, written (?=...), checks that something appears ahead in the string without consuming any characters. Because it consumes nothing, several lookaheads can sit side by side at the start of a pattern and each one scans the whole password independently. The first asks "is there a lowercase letter anywhere?", the second starts again from the same position and asks "is there an uppercase letter anywhere?", and so on.

Inside each lookahead the shape is (?=.*[a-z]): .* means "any run of characters" and [a-z] is what must appear somewhere in that run. Swap the character class and you swap the rule. One lookahead per requirement, each easy to read, test and change on its own.

The five pieces of the pattern

  • Length: .{8,} matches 8 or more characters; wrapped in anchors as ^.{8,}$, it accepts any string that long.
  • Lowercase: (?=.*[a-z]) requires at least one letter from a to z.
  • Uppercase: (?=.*[A-Z]) requires at least one letter from A to Z.
  • Digit: (?=.*\d) requires at least one character from 0 to 9. \d is the digit class.
  • Symbol: (?=.*[^A-Za-z0-9]) requires at least one character that is not a letter or a digit. The ^ inside the brackets negates the class, so punctuation, spaces and everything count as symbols.

Assembled, with ^ at the start and $ at the end so the entire string is judged, the pattern is ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{8,}$. The order of the lookaheads does not matter, because each one rescans the string. The .{8,} at the end consumes the characters and enforces the length.

How to build and test the pattern, step by step

  1. Open the Regex Tester and type the first piece only, ^.{8,}$, in the Pattern field, without slashes. Leave the Flags field empty: an anchored pattern needs no flags, and without g the tester reports the first match, which is all you need.
  2. Type a test password in the text field. sunshine is 8 characters, so the match list shows one match covering the string. Shorten it to sun and the match disappears: the length rule works.
  3. Add the lowercase lookahead so the pattern reads ^(?=.*[a-z]).{8,}$, then the uppercase one, then the digit, then the symbol, testing after each addition. A password that just failed tells you the new lookahead is the rule it breaks.
  4. Read the match list: each match shows its text, its index and any capture groups. For this pattern, one match starting at index 0 and spanning the password means a pass; no match means a fail.

Worked example: four passwords against the full pattern

Run the finished pattern ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{8,}$ against four candidates:

  • sunshine fails. It has the length and lowercase letters, but no uppercase letter, no digit and no symbol.
  • Sunshine fails. Length, lowercase and uppercase are covered; the digit and symbol are still missing.
  • Sunshine1 fails, by one rule only. Everything is present except a symbol.
  • Sunshine1! passes. The match list shows one match, the whole string, starting at index 0.

Notice what the tester does not tell you: it reports match or no match, never which rule failed. A flat "no match" for Sunshine1 gives no hint that the symbol is the only problem.

How to find which rule a password failed

Test the lookaheads one at a time, each as a complete pattern. For the digit rule, use ^(?=.*\d).+$: the lookahead checks for a digit and .+ consumes the string so a match can be reported. Build the same shape for the other rules: ^(?=.*[a-z]).+$, ^(?=.*[A-Z]).+$ and ^(?=.*[^A-Za-z0-9]).+$, and test length separately with ^.{8,}$.

Against Sunshine1, the lowercase, uppercase, digit and length patterns all match, and the symbol pattern alone reports no match. That is the diagnosis: add any symbol and the password passes. This routine works on any stacked-lookahead pattern, not just this one. It is also how you safely relax a policy: if symbols become optional, delete that one lookahead, retest the set, and nothing else changes.

What a composition regex cannot check

This pattern verifies composition: the ingredients present in the string. It says nothing about quality. Password123! passes all five rules and is a bad password, because it is built from a common word, a predictable number run and a symbol tacked on the end, exactly the shape guessing tools try first. A composition check also cannot know whether a password has appeared in a data breach, contains the user's name or birthday, or is reused on twelve other sites.

So treat the regex as a floor, not a verdict. It is a reasonable way to reject empty, tiny or single-class passwords at a form. Real strength comes from length, unpredictability and uniqueness, and none of those three can be read off a character-class checklist. If you write the policy, say so to your users instead of implying a tick from the regex means their password is safe.

Common mistakes

  • Forgetting the anchors. Without ^ and $, the engine can satisfy the pattern on a slice of the string, and passwords pass that should not.
  • Writing the pattern with slashes. In the tester, type the pattern body only and put flags in their field. Slashes become literal characters the pattern hunts for.
  • Testing with the g flag and getting confused. A full-string anchored pattern can only match once. Leave flags empty here; the tester reports the first match, which is the answer you need.
  • Letting .* surprise you across lines. . does not match a newline unless the s flag is set. Passwords are single-line, so it rarely matters here, but it explains odd results when you reuse the technique on multi-line text.
  • Believing a pass means strong. Composition is not strength. See the section above before you ship the policy.
  • Over-tightening the symbol rule. [^A-Za-z0-9] counts a space as a symbol. If your policy means punctuation specifically, narrow the class to the characters you accept, such as [!@#$%], and test again.

FAQs

What is the full regex for a password with letters, numbers and a symbol? ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{8,}$ requires at least 8 characters, one lowercase letter, one uppercase letter, one digit and one character that is not a letter or digit. Each rule sits in its own lookahead, so you can add, drop or relax rules one at a time.

How do I find out which rule a password failed? Test the lookaheads one at a time. Use ^(?=.*[a-z]).+$ on its own, then the uppercase, digit and symbol versions. The first pattern that reports no match names the missing ingredient.

Why does my pattern need ^ and $ anchors? Without anchors, the pattern can match a slice of a longer string, so a password can pass on the strength of a few characters. Anchors force the whole string to satisfy the length rule and every lookahead.

Does passing this regex mean a password is strong? No. The regex only checks composition. Password123! passes every rule here and is still a poor password because it is predictable. Composition checks cannot judge guessability or whether a password appears in a breach list.

Do I type the pattern with slashes in the tester? No. Type just the pattern body, like \d{3}-\d{4}, and put any flags in their own field. For an anchored full-string pattern like this one you need no flags at all.

Conclusion

A password policy pattern is five checks wearing one coat: four lookaheads for the required ingredients and a length rule to consume the string. Build it one lookahead at a time in the Regex Tester, keep a set of passwords that fail exactly one rule each, and you can prove the pattern works and diagnose any rejection in seconds. For the neighbouring question of why a quantifier inside a pattern grabs more text than you expected, see What Is the Difference Between Greedy and Lazy Regex Quantifiers. More guides live in the Developer Tools guides archive.