'PowerShell 5.1 - What is another way to concatenate my array values

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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1