S SmartDocs
系列: python old python 10 行 · 更新於 2026-02-03

text_utils.py

python_old/pytest-workshop/pytest-workshop-solutions/src/text_utils.py

import re

def slugify(text: str) -> str:
    s = text.strip().lower()
    if not s:
        return ""
    s = re.sub(r"\s+", "-", s)
    s = re.sub(r"[^a-z0-9\-]", "", s)
    s = re.sub(r"-{2,}", "-", s).strip("-")
    return s

相關文章