Python (re)

Positive Lookahead (Password) in PY

Use positive lookahead `(?=...)` to require the password contain at least one digit and one uppercase letter.

Try it in the PY tester →

Pattern

regexPY
^(?=.*\d)(?=.*[A-Z])[A-Za-z\d]{8,}$

Python (re) code

pyPython
import re

pattern = re.compile(r"^(?=.*\d)(?=.*[A-Z])[A-Za-z\d]{8,}$")
input_text = "Password1"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

How the pattern works

(?=.*\d) is a zero-width assertion that requires the rest of the string to contain a digit somewhere. (?=.*[A-Z]) similarly requires an uppercase letter. Neither consumes characters. [A-Za-z\d]{8,}$ then matches the actual password content (≥8 alphanumerics). Lookaheads are supported in JS and Python; Go's RE2 does NOT support them.

Examples

Input

Password1

Matches

  • Password1

Input

weakpass

No match

Input

ALLCAPSNODIGIT

No match

Same pattern, other engines

← Back to Positive Lookahead (Password) overview (all engines)