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.
The pattern
//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
^Start anchor — asserts position at the beginning of the string (or line with the 'm' flag). Consumes no characters.(?=.*[a-z])Positive lookahead — requires any character followed by […] to follow, but does not consume it.Dot — matches any single character (except line terminators unless the 's' flag is set)*The preceding element matches zero or more times (greedy)[a-z]Character class — matches one character from: 'a' through 'z'(?=.*[A-Z])Positive lookahead — requires any character followed by […] to follow, but does not consume it.Dot — matches any single character (except line terminators unless the 's' flag is set)*The preceding element matches zero or more times (greedy)[A-Z]Character class — matches one character from: 'A' through 'Z'(?=.*\d)Positive lookahead — requires any character followed by a digit (0–9) to follow, but does not consume it.Dot — matches any single character (except line terminators unless the 's' flag is set)*The preceding element matches zero or more times (greedy)\dMatches a digit (0–9)(?=.*[^\w\s])Positive lookahead — requires any character followed by [^…] to follow, but does not consume it.Dot — matches any single character (except line terminators unless the 's' flag is set)*The preceding element matches zero or more times (greedy)[^\w\s]Character class — matches any single character NOT in: a word character (letter, digit, or underscore), a whitespace character (space, tab, newline, etc.).Dot — matches any single character (except line terminators unless the 's' flag is set){12,}The preceding element matches at least 12 times (greedy)$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());
}
}