Python (re)

Username in PY

Validate usernames that are 3–16 characters long and contain only letters, digits, underscores, and hyphens.

Try it in the PY tester →

Pattern

regexPY
^[a-zA-Z0-9_\-]{3,16}$

Python (re) code

pyPython
import re

pattern = re.compile(r"^[a-zA-Z0-9_\-]{3,16}$")
input_text = "john_doe"
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

^ and $ anchor to the full string. The character class [a-zA-Z0-9_\-] allows letters (upper and lower), digits, underscores, and hyphens. {3,16} enforces the length range most platforms use for usernames.

Examples

Input

john_doe

Matches

  • john_doe

Input

user-123

Matches

  • user-123

Input

ab

No match

Same pattern, other engines

← Back to Username overview (all engines)