4

Given:

  • csv integers 100,200
  • PowerShell 5.1

I'd like to take the csv integers and make it look like the following:

$parmIn = "100,200"
desired output: "(100),(200)"

The way it is currently done:

   $parmIn = "100,200"
   $x = "(" + "$parmIn".replace(",", "),(") + ")"

What's another way to write this more concisely in PowerShell?
I was thinking something with array subexpressions or something.

1 Answer 1

5

Use the regex-based -replace operator:

 "100,200" -replace '\d+', '($&)'  # -> "(100),(200)"
  • Regex \d+ matches one or more (+) decimal digits (\d)

  • In the replacement expression, $& refers to what each \d+ match captured.

Sign up to request clarification or add additional context in comments.

Comments

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.