This library is abandoned. Please consider using a different library.

A pattern is composed from one or more segments delimited by separators.

Examples

Pattern Separator Segments
/a/b/c / a, b, c
a/b / a, b
a.b.c/d . a, b, c/d

Segments

A segment can be one of the following:

When a segment contains only one optional placeholder and nothing more, then the separator symbol character also becomes optional, depending on the capture mode.

The pattern a//{p}/{p1?}/test-{p2}-for-{p3?} has the following segments

  • a - data
  • empty data
  • {p} - placeholder
  • {p1?} - optional placeholder
  • test-{p2}-for-{p3?} - combination of data and placeholders

Above, the separator symbol used was / (slash), use the separator symbol option to change it.

Data

Data inside a segment is automatically escaped, depending on the regex delimiter option.

Examples

Delimiter Data Escaped
~ a~b a\~b
# a#b a\#b

Placeholder

The placeholder is always delimited by the start symbol and the end symbol. To change the placeholder delimiters please use those options.

Any placeholder is composed from:

If a placeholder doesn’t have a name, it is considered to be anonymous and must have an inline regex. An anonymous placeholder can also be marked as optional.

Examples

Placeholders Name Optional Regex
{var} var No default
{opt?} opt Yes default
{num=\d+} num No \d+
{num?=[a-z]{5,10}} num Yes [a-z]{5,10}
{=[abc]*} anonymous No [abc]*
{?=[abc]+} anonymous Yes [abc]+

Placeholder name

The placeholder name must begin with a letter, and after the first letter can contain any number of letters, digits or underscores.

The name of the placeholder must be unique across the pattern.

Examples

Pattern Valid Reason
{abc} Yes  
{A1B2c3} Yes  
{a_1_B_2} Yes  
{=\d} Yes  
{?=[a-z]{2, 3}} Yes  
{123A} No Must start with a letter
{_a_b} No Must start with a letter
{A-B} No Contains - (minus sign)
{?=} No Must have inline regex
{} No Must have inline regex
{=} No Must have inline regex
{a}/{b}/{a} No Duplicate name a
{a}/{b}/{A} Yes  

Optional placeholder

A placeholder can be marked as optional using the optional symbol right after the placeholder name.

If the placeholder is optional and is the only thing in the segment, it will also make the adjacent separator optional, depending on the capture mode.

Example

To make {var} optional, just add ? after the name, resulting {var?}.

Inline regex

A placeholder can contain an inline regex using the assign symbol right after the placeholder name if the placeholder is not anonymous, or after the optional symbol if the placeholder is marked as optional.

If a named placeholder doesn’t have an inline regex, it uses the default regex expression.

Examples

To add an inline regex for {var}, add = and the regex, just after the name, resulting {var=my-regex}.

To add an inline regex for {var?}, add = and the regex, just after the ?, resulting {var?=my-regex}.

To add an anonymous placeholder with inline regex, use {=my-regex} or {?=my-regex} if you want to mark this placeholder as optional.