PathPattern class final

A URL path pattern, such as /books/{isbn} or /static/**, parsed once and then tested against request paths.

Every path an application writes for the web server is one of these: the paths of @GetMapping and the other mappings, of CORS mappings, of interceptors, of security rules, of WebSocket handlers. The framework parses them; applications rarely use the class directly, except in their own filters or when testing what a pattern matches.

Syntax Matches
/books the path itself
? one character of a segment
* zero or more characters of a segment
** zero or more segments, at the end only (/static/**)
{isbn} one segment, captured as isbn
{isbn:\d+} one segment matching the regex, captured
{*rest} the rest of the path, captured with its leading / (end only)

Paths are compared segment by segment, each percent-decoded, and captured values are decoded. Matrix parameters are left out of the comparison, as in Spring: /books/1111;v=1 matches /books/{isbn} with isbn 1111 (an encoded %3B stays part of the value). A trailing slash matters: /books/ does not match /books, as in Spring 6. When several patterns match, the more specific one should win; sorting patterns (compareTo) puts it first.

// A filter that only guards the admin API.
final admin = PathPattern.parse('/api/admin/**');
if (admin.matches(request.path)) { ... }

final book = PathPattern.parse('/books/{isbn}');
book.match('/books/978-0')?.variables; // {isbn: 978-0}
book.matches('/books');                // false

Spring (Java): PathPattern.

PathPattern book = PathPatternParser.defaultInstance.parse("/books/{isbn}");
book.matchAndExtract(PathContainer.parsePath("/books/978-0"))
    .getUriVariables(); // {isbn=978-0}
Implemented types

Constructors

PathPattern.parse(String pattern)
Parses pattern. A missing leading / is added, and an empty pattern is /. Throws a FormatException for a malformed pattern: ** or {*name} before the end, unbalanced braces, an invalid regex, or a variable used twice.
factory

Properties

hashCode → int
The hash code for this object.
no setteroverride
isLiteral → bool
Whether the pattern is a fixed path, with no wildcards and no variables.
no setter
pattern → String
The pattern as written, with a leading / added if it had none.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
variableNames → List<String>
The names of the variables the pattern captures, in order: [id, isbn] for /authors/{id}/books/{isbn}.
no setter

Methods

combine(PathPattern other) → PathPattern
This pattern followed by other: /api and /books/{isbn} give /api/books/{isbn}. An empty side gives the other one. The server uses it to join a controller's base path and a method's path.
compareTo(PathPattern other) → int
Compares this object to another object.
override
match(String path) → PathMatch?
Matches path, the raw path of a request (percent-encoded, without the query string). Returns the captured variables, or null when the path does not match.
matches(String path) → bool
Whether path matches the pattern; see match for the format of path.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
override

Operators

operator ==(Object other) → bool
The equality operator.
override