namedGroupMap function

Map<String, String> namedGroupMap(
  1. RegExpMatch match
)

Returns the named capture groups of match as a map of name to value.

Only groups that actually captured (non-null) are included, so optional groups that did not participate are omitted. Returns an empty map when the pattern has no named groups.

Example:

final m = RegExp(r'(?<y>\d{4})').firstMatch('2026')!;
namedGroupMap(m); // {'y': '2026'}

Implementation

Map<String, String> namedGroupMap(RegExpMatch match) {
  final Map<String, String> out = <String, String>{};
  for (final String name in match.groupNames) {
    final String? value = match.namedGroup(name);
    if (value != null) out[name] = value;
  }
  return out;
}