private function formatPattern(string $segment, array $matches): string
    {
        $fullPattern = $this->appendPattern('', $segment, 0, $matches[0][0][1]);

        foreach ($matches as $i => $match) {
            $name = $match['name'][0];
            $pattern = $match['pattern'][0] ?? '.*';

            $this->format .= '%s';

            if (\in_array($name, $this->parameterNames, true)) {
                throw new \InvalidArgumentException("Duplicate parameter name '$name'");
            }

            if (!$this->isValidPattern($pattern)) {
                throw new \InvalidArgumentException("Invalid regular expression '$pattern'");
            }

            $this->parameterNames[] = $name;
            $fullPattern .= sprintf("(?'%s'%s)", $name, $pattern);

            $start = $match[0][1] + \strlen($match[0][0]);
            $length = ($matches[$i + 1][0][1] ?? \strlen($segment)) - $start;
            $fullPattern = $this->appendPattern($fullPattern, $segment, $start, $length);
        }

        $this->format .= '/';

        return $fullPattern;
    }