ThePoShWolf/Curl2PS

Improve parameter value regex to better detect strings with spaces

Closed this issue · 0 comments

Currently the regex to match parameter values isn't all that great:

if ($workingStr -match '\s\-\S'){
        $index = $workingStr.IndexOf($Matches[0])
    } else {
        $index = $workingStr.Length
    }
}

It finds the index of the next - which it assumes means the next parameter start, which works in most cases, but doesn't account for a parameter value that may contain something like: --data-urlencode 'value=Example -fail'

Even though that string is wrapped in '' it fails.

Not only that, but the current method uses .SubString(0,$index) to pull the actual value, which isn't as fast or cool as using regex.

I'm thinking the replacement regex will be something along the lines of:

$escapedParamName = [regex]::Escape($parameterName)
$workingStr -match "$escapedParamName (?<paramValue>`'[^']`'|`"[^`"]`"|[^\s])"
$matches.paramValue

That definitely needs to be tested, but the general idea is to account for quotes.