private function getVarRegex(ParsedRoute $parsedRoute, string $segment) : string
    {
        if (preg_match(self::$variableMatchingRegex, $segment, $matches) !== 1) {
            throw new RouteException("Variable name can't be empty");
        }

        $variableName = $matches[1];
        $defaultValue = $matches[2] ?? '';

        if (strlen($variableName) > self::VARIABLE_MAXIMUM_LENGTH) {
            throw new RouteException(sprintf(
                'Variable name "%s" cannot be longer than %d characters. Please use a shorter name.',
                $variableName,
                self::VARIABLE_MAXIMUM_LENGTH)
            );
        }

        if (in_array($variableName, $this->variableNames)) {
            throw new RouteException("Route uses multiple references to \"$variableName\"");
        }

        $this->variableNames[] = $variableName;
        $parsedRoute->setDefaultValue($variableName, $defaultValue);
        $variableRegex = $parsedRoute->getVarRegex($variableName);

        if ($variableRegex === null) {
            // Add a default regex
            $variableRegex = "[^\/:]+";
        }

        $this->cursor += mb_strlen($matches[0]);

        return sprintf('(?P<%s>%s)', $variableName, $variableRegex);
    }