Named parameters are defined by prefixing a colon to a segment of word characters [A-Za-z0-9_] like :user.
Additional modifiers can be suffixed for different meaning:
It is possible to write an unnamed parameter that only consists of a matching group. It works the same as a named parameter, except it will be numerically indexed.
let template = PathTemplate("/User/:id", options: .init(isCaseSensitive: true))
template.extract("/user/123") // Note that "user" has a lowercase "u"
=> []
Accessing the underlying path regular expression
let template: PathTemplate = "/user/:id"
template.regex
=> <NSRegularExpression: 0x102745860> ^\/user\/([^\/]+?)(?:\/)?$
PathTemplate
Swift implementation of templated paths. Inspired by path-to-regexp and URITemplate.swift.
Installation
Swift Package Manager:
Append the following to your
Package.swift
dependencies: []
Cocoapods
Add the following to your
Podfile
Basic Usage
A
PathTemplate
is useful for working on structuredString
data like URLs.Expanding a path template
Determine the parameters in a template
Extract the parameters used in a given path
Parameter Modifiers
Named parameters are defined by prefixing a colon to a segment of word characters
[A-Za-z0-9_]
like:user
. Additional modifiers can be suffixed for different meaning:?
the parameter is optional*
zero or more segments+
one or more segments?
Optional*
Zero or More+
One or MoreRegular Expression Parameters
Parameters can be provided a custom regular expression that overrides the default match. For example, you could enforce matching digits in a path.
Unnamed Parameters
It is possible to write an unnamed parameter that only consists of a matching group. It works the same as a named parameter, except it will be numerically indexed.
Advanced Usage
Enforcing case sensitive matching
Accessing the underlying path regular expression