0

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.

1
  • How is it related to PowerShell? Commented Dec 8, 2010 at 6:00

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you that worked. There's an additional requirement though.. If the arguments look like the following: -car:"Nissan" -Model:"Dina" -Color:"Light-blue" -wheels:"4" -windowSize.Front:"24" -windowSize.Back:"26" How can the regex be changed to cater for these
@Henno, I have updated the answer so that it includes the new case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.