RegexCraft

Password strength regex

Checks a password policy with four lookaheads: at least one lowercase letter, one uppercase letter, one digit and one symbol, and at least 12 characters overall. Each line is tested on its own.

Open in the tester

The pattern

/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w\s]).{12,}$/gm

Sample text, 1 match

Tr0ub4dor&3xyz correcthorsebattery Short1! N0symbolsHere12

What it deliberately doesn’t do

  • Lookaheads are not supported by Go or Rust's regex crate; check each rule in code there.
  • Length matters more than character mix. Pair this with a check against known breached passwords.

Explained, in order

  1. ^Start anchor — asserts position at the beginning of the string (or line with the 'm' flag). Consumes no characters.
  2. (?=.*[a-z])Positive lookahead — requires any character followed by […] to follow, but does not consume it
  3. .Dot — matches any single character (except line terminators unless the 's' flag is set)
  4. *The preceding element matches zero or more times (greedy)
  5. [a-z]Character class — matches one character from: 'a' through 'z'
  6. (?=.*[A-Z])Positive lookahead — requires any character followed by […] to follow, but does not consume it
  7. .Dot — matches any single character (except line terminators unless the 's' flag is set)
  8. *The preceding element matches zero or more times (greedy)
  9. [A-Z]Character class — matches one character from: 'A' through 'Z'
  10. (?=.*\d)Positive lookahead — requires any character followed by a digit (0–9) to follow, but does not consume it
  11. .Dot — matches any single character (except line terminators unless the 's' flag is set)
  12. *The preceding element matches zero or more times (greedy)
  13. \dMatches a digit (0–9)
  14. (?=.*[^\w\s])Positive lookahead — requires any character followed by [^…] to follow, but does not consume it
  15. .Dot — matches any single character (except line terminators unless the 's' flag is set)
  16. *The preceding element matches zero or more times (greedy)
  17. [^\w\s]Character class — matches any single character NOT in: a word character (letter, digit, or underscore), a whitespace character (space, tab, newline, etc.)
  18. .Dot — matches any single character (except line terminators unless the 's' flag is set)
  19. {12,}The preceding element matches at least 12 times (greedy)
  20. $End anchor — asserts position at the end of the string (or line with the 'm' flag). Consumes no characters.

In your language

JavaScript

The engine this page was tested with.

const re = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w\s]).{12,}$/gm;

// Example
const matches = [...str.matchAll(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w\s]).{12,}$/gm)];
for (const m of matches) {
  console.log(m[0], m.index, m.groups);
}

Python

Translated. Named groups are rewritten to (?P<name>…).

import re

pattern = re.compile(r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w\s]).{12,}$", re.MULTILINE)

# Example
for m in pattern.finditer(text):
    print(m.group(0), m.start(), m.groups())

Go

Translated. Go's RE2 has no lookaround or backreferences.

package main

import (
	"fmt"
	"regexp"
)

// Warning: Go's RE2 does not support lookarounds (?=, ?!, ?<=, ?<!).
// Note: Inline flags at the start of the pattern: (?i), (?m), (?s).

var re = regexp.MustCompile(`(?m)^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w\s]).{12,}$`)

func main() {
	for _, match := range re.FindAllStringSubmatch(text, -1) {
		fmt.Println(match[0], match)
	}
}

Rust

Translated. The regex crate has no lookaround or backreferences.

use regex::Regex;

// Warning: The `regex` crate (RE2) does not support lookarounds.
// Note: Rust finds all matches via find_iter / captures_iter (no 'g' flag).
// Note: Use (?s), (?m), (?u) inline flags or the RegexBuilder API.

fn main() {
    let re = Regex::new(r#"(?m)^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w\s]).{12,}$"#).unwrap();

    for m in re.find_iter(text) {
        println!("{} at {}", m.as_str(), m.start());
    }
}