경로 패턴('/user/:id' 등)을 라우팅 매칭을 위해 정규 표현식으로 변환하는 Dart 패키지입니다. 파라미터 캡처, 와일드카드, 선택적 세그먼트를 지원하여 동적 경로 분석이 가능합니다. Flutter 앱에서 라우팅 시스템을 구축하는 데 이상적이며, 정의된 경로와 URL을 매칭하기 위한 가벼운 솔루션을 제공합니다. 이 패키지는 모든 주요 플랫폼과 호환되며 기존 네비게이션 로직과 원활하게 통합됩니다.
사용 사례
다이나믹 루트 매칭
URL 패턴 분석
내비게이션 시스템 구현
경로 매개변수 추출
사용자 정의 라우터 개발
API 엔드포인트 일치
주요 기능
이름 있는 매개변수(예: :id)를 지원합니다
선택적 세그먼트 처리
와일드카드 매칭 포함
재사용 가능한 RegExp 객체를 생성합니다
경량이고 빠름
크로스 플랫폼 호환성
적합한 대상
사용자 지정 라우터를 구축하는 개발자
유연한 URL 라우팅이 필요한 앱
동적 경로를 사용한 프론트엔드 탐색
복잡한 경로 패턴 분석
기존 라우팅 라이브러리와의 통합
고려 사항
쿼리 매개변수를 처리하지 않습니다
내장 미들웨어 지원 없음
수동 경로 해결이 필요합니다
경로 수준 일치에 제한됨
완전한 HTTP 서버 사용을 위한 설계가 아님
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
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