Python (re)

Slug (Unicode Letters Allowed) in PY

Python (re) can't run this pattern out of the box.

Try it in the PY tester →

Why it doesn't work in PY

CPython's stdlib `re` doesn't support `\p{...}` Unicode property escapes (those need the third-party `regex` package).

Approach

Replace `\p{L}` with explicit Unicode ranges, or drop the `u` flag if you only need ASCII matching.

Workaround code in Python (re)

pyPython (workaround)
# Option A: install the third-party `regex` package which DOES
# support \p{...} property escapes (drop-in replacement for `re`).
#
#   pip install regex
import regex
pattern = regex.compile(r'^[\p{L}\p{N}]+(?:-[\p{L}\p{N}]+)*$', regex.UNICODE)
matches = pattern.findall("café-rénové hello-world")
print(matches)


# Option B: stay on stdlib `re` and use explicit Unicode ranges for
# the categories you need. Below covers Latin letters + Latin-1 +
# Latin-Extended (handles café, naïve, résumé, etc.).
import re
pattern = re.compile(
    r'[A-Za-z\u00C0-\u024F\u1E00-\u1EFF]+',
    re.UNICODE,
)
matches = pattern.findall("café-rénové hello-world")
print(matches)

Two paths: install the `regex` package (full \p{} support) or stay on stdlib `re` with explicit Unicode block ranges.

Pattern

regexPY
^[\p{L}\p{N}]+(?:-[\p{L}\p{N}]+)*$   (flags: u)

How the pattern works

[\p{L}\p{N}]+ matches one or more Unicode letters or numbers (so `café` and `東京` are valid). (?:-[\p{L}\p{N}]+)* allows additional hyphen-separated segments. The u flag is REQUIRED in JavaScript for \p{} property escapes; Python's stdlib `re` doesn't support \p{} (use \w with Unicode flag, or the `regex` package); Go RE2 has its own \p{...} support.

Examples

Input

hello-world

Matches

  • hello-world

Input

café-rénové

Matches

  • café-rénové

Input

trailing-

No match

Same pattern, other engines

← Back to Slug (Unicode Letters Allowed) overview (all engines)