Python (re)

URL Slug in PY

Validate URL slugs: lowercase letters and digits separated by single hyphens, no leading/trailing hyphens.

Try it in the PY tester →

Pattern

regexPY
^[a-z0-9]+(?:-[a-z0-9]+)*$

Python (re) code

pyPython
import re

pattern = re.compile(r"^[a-z0-9]+(?:-[a-z0-9]+)*$")
input_text = "my-blog-post"
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

Anchored with ^ and $. Requires at least one alphanumeric character. Hyphens can only appear between alphanumeric groups — not at start or end.

Examples

Input

my-blog-post

Matches

  • my-blog-post

Input

hello-world-123

Matches

  • hello-world-123

Input

-bad-slug-

No match

Same pattern, other engines

← Back to URL Slug overview (all engines)