Package selection guidance compiled from public package information, community data, and project usage.
A Dart package that converts path patterns like '/user/:id' into regular expressions for route matching. It enables dynamic path parsing by supporting parameter capture, wildcards, and optional segments. Ideal for building routing systems in Flutter apps, it provides a lightweight solution for matching URLs against defined routes. The package is compatible across all major platforms and integrates seamlessly with existing navigation logic.
Use cases
Dynamic route matching
URL pattern parsing
Navigation system implementation
Route parameter extraction
Custom router development
API endpoint matching
Key features
Supports named parameters (e.g., :id)
Handles optional segments
Includes wildcard matching
Generates reusable RegExp objects
Lightweight and fast
Cross-platform compatibility
Suitable for
Developers building custom routers
Apps needing flexible URL routing
Frontend navigation with dynamic paths
Parsing complex route patterns
Integrating with existing routing libraries
Considerations
Does not handle query parameters
No built-in middleware support
Requires manual route resolution
Limited to path-level matching
Not designed for full HTTP server use
Original README
Original README snapshot from the linked source repository. Visit the repository for the latest content.
path_to_regexp
Converts a path such as /user/:id into a regular expression.
Matching
pathToRegExp() converts a path specification into a regular expression that
matches conforming paths.
By default, parameters match anything until the next delimiter. This behavior
can be customized by specifying a regular expression in parentheses following
a parameter name.
Parameters can be extracted from a path specification during conversion into a
regular expression.
final parameters = <String>[];
final regExp = pathToRegExp('/user/:id', parameters: parameters);
parameters; // => ['id']
Extracting Arguments
extract() maps the parameters of a path specification to their corresponding
arguments in a match.
final parameters = <String>[];
final regExp = pathToRegExp('/user/:id', parameters: parameters);
final match = regExp.matchAsPrefix('/user/12');
extract(parameters, match); // => {'id': '12'}
Generating
pathToFunction() converts a path specification into a function that generates
matching paths.
final toPath = pathToFunction('/user/:id');
toPath({'id': '12'}); // => '/user/12'
Tokens
parse() converts a path specification into a list of tokens, which can be
used to create a regular expression or path generating function.
final tokens = parse('/users/:id');
final regExp = tokensToRegExp(tokens);
final toPath = tokensToFunction(tokens);
Similar to pathToRegExp(), parameters can also be extracted during parsing.
final parameters = <String>[];
final tokens = parse('/users/:id', parameters: parameters);
If you intend to match and generate paths from the same path specification,
parse() and the token-based functions should be preferred to their path-based
counterparts. This is because the token-based functions can reuse the same
tokens, whereas each path-based function must parse the path specification anew.
Options
Prefix Matching
By default, a regular expression created by pathToRegExp or tokensToRegExp
matches the entire input. However, if the optional prefix argument is true, it
may also match as a prefix until a delimiter.
final regExp = pathToRegExp('/user/:id', prefix: true);
regExp.hasMatch('/user/12/details'); // => true
Case Sensitivity
By default, a regular expression created by pathToRegExp or tokensToRegExp
is case sensitive. To create a case insensitive regular expression, set
caseSensitive to false.
final regExp = pathToRegExp('/user/:id', caseSensitive: false);
regExp.hasMatch('/USER/12'); // => true