Go (RE2)

OpenAPI Path Template in GO

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

Try it in the GO tester →

Pattern

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

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`\/[\w\-]+(?:\/(?:\{[\w\-]+\}|[\w\-]+))*\/?`)
	input := `GET /users/{id}/posts/{postId}/comments`
	for _, match := range re.FindAllString(input, -1) {
		fmt.Println(match)
	}
}

Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.

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)