开发者

Parsing custom arguments in Powershell "-" delimitted

I have a string

-car:"Nissan" -Model:"Dina" -Color:"Light-blue" -wheels:"开发者_如何学编程4"

How can I extract the arguments? Initial thoughts was to use the '-' as the delimiter, however that's not going to work.


Use of a regular expression is probably the easiest solution of the task. This can be done in PowerShell:

$text = @'
-car:"Nissan" -Model:"Dina" -Color:"Light-blue" -wheels:"4" -windowSize.Front:"24"
'@

# assume parameter values do not contain ", otherwise this pattern should be changed
$pattern = '-([\.\w]+):"([^"]+)"'

foreach($match in [System.Text.RegularExpressions.Regex]::Matches($text, $pattern)) {
 $param = $match.Groups[1].Value
 $value = $match.Groups[2].Value
 "$param is $value"
}

Output:

car is Nissan
Model is Dina
Color is Light-blue
wheels is 4
windowSize.Front is 24
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜