private function convertRawStringToRegex(ParsedRoute $parsedRoute, string $rawString) : string
    {
        if (empty($rawString)) {
            return '#^.*$#';
        }

        $this->variableNames = [];
        $bracketDepth = 0;
        $this->cursor = 0;
        $rawStringLength = mb_strlen($rawString);
        $regex = '';

        while ($this->cursor < $rawStringLength) {
            $char = $rawString[$this->cursor];

            switch ($char) {
                case ':':
                    $regex .= $this->getVarRegex($parsedRoute, mb_substr($rawString, $this->cursor));
                    break;
                case '[':
                    $regex .= '(?:';
                    $bracketDepth++;
                    $this->cursor++;
                    break;
                case ']':
                    $regex .= ')?';
                    $bracketDepth--;
                    $this->cursor++;
                    break;
                default:
                    $regex .= preg_quote($char, '#');
                    $this->cursor++;
            }
        }

        if ($bracketDepth != 0) {
            throw new RouteException(
                sprintf('Route has %s brackets', $bracketDepth > 0 ? 'unclosed' : 'unopened')
            );
        }

        return sprintf('#^%s$#', $regex);
    }