private function generateUrlPart(string $rawPart, string $regex, string $routeName, &$values) : string
    {
        $generatedPart = $rawPart;
        $count = 1000;

        while ($count > 0 && count($values) > 0) {
            $generatedPart = preg_replace(self::$variableMatchingRegex, $values[0], $generatedPart, 1, $count);

            if ($count > 0) {
                // Only remove a value if we actually replaced something
                array_shift($values);
            }
        }

        // Remove any leftover brackets or variables
        $generatedPart = preg_replace(self::$leftoverBracketsAndVariablesRegex, '', $generatedPart);

        // Make sure what we just generated satisfies the regex
        if (!preg_match($regex, $generatedPart)) {
            throw new UrlException(
                "Generated URL part \"$generatedPart\" does not satisfy regex for route \"$routeName\""
            );
        }

        return $generatedPart;
    }