Skip to content

[go_router] docs: Document regex constraints on path parameters (flutter#177766)#11722

Draft
NadeemIqbal wants to merge 2 commits into
flutter:mainfrom
NadeemIqbal:nadeem/177766-go-router-regex-docs
Draft

[go_router] docs: Document regex constraints on path parameters (flutter#177766)#11722
NadeemIqbal wants to merge 2 commits into
flutter:mainfrom
NadeemIqbal:nadeem/177766-go-router-regex-docs

Conversation

@NadeemIqbal
Copy link
Copy Markdown

Fixes flutter/flutter#177766.

Description

GoRoute.path supports constraining a path parameter to a regular expression via the :name(regex) syntax (parsed in packages/go_router/lib/src/path_utils.dart by _parameterRegExp and patternToRegExp), but the feature was undocumented. As the reporter noted on the issue, the feature is currently only discoverable by trawling related GitHub issues (flutter/flutter#143070, flutter/flutter#107240, flutter/flutter#117307). On the issue thread @justinmc confirmed the addition "sounds reasonable to me".

This PR adds a paragraph to GoRoute.path's dartdoc that:

  • Describes the supported :paramName(regex) syntax.
  • Documents the matching semantics: a segment that fails the regex falls through to other route candidates.
  • Calls out the practical constraint that the user-supplied expression must not introduce its own capturing groups (the implementation wraps the expression in a named group).

Documentation-only change; no API or behavior change.

List which issues are fixed by this PR.

flutter/flutter#177766

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter.
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation.
  • I am willing to follow-up on review comments in a timely manner.

GoRoute.path supports constraining a path parameter to a regular expression
via the :name(regex) syntax (parsed in path_utils.dart by _parameterRegExp
and patternToRegExp), but the feature was undocumented; users found it only
by trawling issues such as flutter/flutter#143070 and flutter/flutter#107240.

Add a paragraph to GoRoute.path's dartdoc describing the supported syntax,
explaining the matching semantics (segments that fail the pattern fall
through), and noting the constraint that the regex must not introduce its
own capturing groups.

Fixes flutter/flutter#177766.
@github-actions github-actions Bot added p: go_router triage-framework Should be looked at in framework triage labels May 17, 2026
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds documentation to GoRoute explaining how to constrain path parameters with regular expressions. Feedback suggests using raw string literals for regex examples to avoid escape sequence errors and clarifying that nested parentheses are not supported by the path parser.

/// `:paramName(regex)`. The parameter only matches if the corresponding path
/// segment also matches the regular expression — segments that fail the
/// pattern fall through to other route candidates. For example,
/// `/users/:id(\d+)` matches `/users/42` but not `/users/alice`. The
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The example path contains a backslash (\d). In Dart, this requires using a raw string literal or escaping the backslash to avoid an 'Invalid escape sequence' error. Using the raw string prefix r in the documentation makes the example more accurate and easier for developers to use directly.

Suggested change
/// `/users/:id(\d+)` matches `/users/42` but not `/users/alice`. The
/// `r'/users/:id(\d+)'` matches `/users/42` but not `/users/alice`. The

Comment thread packages/go_router/lib/src/route.dart Outdated
Comment on lines +370 to +371
/// expression must not introduce its own capturing groups; everything
/// between the parentheses is interpreted as a Dart [RegExp] body.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation of the path parser (_parameterRegExp in path_utils.dart) does not support un-escaped nested parentheses. Additionally, the internal escaping of characters like :, =, and ! prevents the use of advanced features such as non-capturing groups ((?:...)) or lookarounds. It is more accurate to state that the expression must not contain nested parentheses.

Suggested change
/// expression must not introduce its own capturing groups; everything
/// between the parentheses is interpreted as a Dart [RegExp] body.
/// expression must not contain nested parentheses; everything
/// between the parentheses is interpreted as a Dart [RegExp] body.

…m doc

Address review feedback (flutter#11722): the parser disallows un-escaped nested
parentheses (per `_parameterRegExp`'s `[^\\()]` class) and additionally
escapes `:`, `=`, and `!`, which also prevents non-capturing groups and
lookarounds. Saying the expression "must not contain nested parentheses"
captures the actual constraint more accurately than "must not introduce
its own capturing groups".
@stuartmorgan-g stuartmorgan-g marked this pull request as draft May 18, 2026 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p: go_router triage-framework Should be looked at in framework triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[go_router] Improve docs about regex support

1 participant