Python (re)

Python Package Name (PEP 508) in PY

Validate Python distribution package names per PEP 508: letters, digits, dots, underscores, hyphens.

Try it in the PY tester →

Pattern

regexPY
^[A-Za-z0-9](?:[A-Za-z0-9._\-]*[A-Za-z0-9])?$

Python (re) code

pyPython
import re

pattern = re.compile(r"^[A-Za-z0-9](?:[A-Za-z0-9._\-]*[A-Za-z0-9])?$")
input_text = "requests"
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

Starts and ends with alphanumeric. Allows `_`, `.`, `-` in the middle. The single-char form (just an alphanumeric) is also valid. PEP 508 / PEP 503 normalize all of `_`, `.`, `-` to a single `-` for matching, but the underlying name on PyPI may use any.

Examples

Input

requests

Matches

  • requests

Input

django-rest-framework

Matches

  • django-rest-framework

Input

_invalid

No match

Same pattern, other engines

← Back to Python Package Name (PEP 508) overview (all engines)