hasMatch method

List<String>? hasMatch({
  1. required String path,
  2. required String pattern,
})

Determines if path matches pattern of the form xxxxx/:id/yyyy.

If no match is found, Null is returned.

If a match is found, the :id portion is returned in the list.

pathxxxx/:id/yyyy形式のpatternにマッチするかを判定します。

マッチしない場合はNullが返されます。

マッチする場合は:idの部分がリストで返されます。

Implementation

List<String>? hasMatch({required String path, required String pattern}) {
  final regExp = RegExp(
    "^${pattern.replaceAllMapped(
          RegExp(r":([^/]+)"),
          (match) => "([^/]+)",
        ).trimString("/")}\$",
  );
  final match = regExp.firstMatch(path);
  if (match == null) {
    return null;
  }
  if (match.groupCount < 1) {
    return [];
  }
  final result = <String>[];
  for (var i = 0; i < match.groupCount; i++) {
    result.add(match.group(i + 1)!);
  }
  return result;
}