Python (re)

OpenAPI Path Template in PY

Match OpenAPI / Express-style path templates with `{param}` (or `:param` after substitution) placeholders.

Try it in the PY tester →

Pattern

regexPY
\/[\w\-]+(?:\/(?:\{[\w\-]+\}|[\w\-]+))*\/?   (flags: g)

Python (re) code

pyPython
import re

pattern = re.compile(r"\/[\w\-]+(?:\/(?:\{[\w\-]+\}|[\w\-]+))*\/?")
input_text = "GET /users/{id}/posts/{postId}/comments"
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

\/[\w\-]+ matches the first path segment. The repeating group (?:\/(?:\{[\w\-]+\}|[\w\-]+))* matches subsequent segments — either literal text or a `{param}` placeholder. \/? allows an optional trailing slash. Useful for path-to-handler routing tables.

Examples

Input

GET /users/{id}/posts/{postId}/comments

Matches

  • /users/{id}/posts/{postId}/comments

Input

/api/v1/health

Matches

  • /api/v1/health

Input

no path

No match

Same pattern, other engines

← Back to OpenAPI Path Template overview (all engines)