path_to_regexp
0.4.0, released 6 years ago by leonsenft.dev
Converts path strings with parameters into regular expressions and generating functions.
What it does
Converts paths like '/user/:id' into regular expressions to match incoming paths or extract parameters. It can also reverse the process by converting a path specification into a function that generates paths from arguments.
AI-written summary of version 0.4.0, based on the package's README and metadata. Report an inaccuracy
Use it when
- You are building a custom routing or deep linking solution and need to match URL paths with parameters.
- You need to generate path strings dynamically from parameter maps.
Skip it when
- You are using a standard framework router like GoRouter or Flutter's Navigator that handles path matching automatically.
Strengths
- Provides bidirectional conversion between paths and regular expressions or generation functions.
- Supports custom parameter regular expressions, prefix matching, and case insensitivity.
- Token-based parsing allows efficient reuse when matching and generating from the same path specification.
Weaknesses
- Optional parameters are not supported (support was removed in version 0.2.0).
Setup
- Kind
- Pure Dart
- Effort
- Low
- Code generation
- No
- Direct dependencies
- 0
final regExp = pathToRegExp('/user/:id');
regExp.hasMatch('/user/12'); // => true
final toPath = pathToFunction('/user/:id');
toPath({'id': '12'}); // => '/user/12'Alternatives
Packages that do the same job and could replace path_to_regexp.
- math_parser
Parse and evaluate mathematical expressions in Dart.
52Compare - hex
Provides simple HEX.encode and HEX.decode functions for converting between byte lists and hexadecimal strings.
47Compare - emoji_regex
Provides a regular expression to match all emoji symbols and sequences as per the Unicode Standard.
47Compare - text_parser
A Dart package for parsing text flexibly using multiple regular expression patterns.
46Compare
Works with
Packages commonly used together with path_to_regexp: ones it builds on, ones built on it, and its own family.