Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

powershell

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.

like image 747
Rod Avatar asked Oct 31 '25 04:10

Rod


1 Answers

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.

like image 116
mklement0 Avatar answered Nov 01 '25 18:11

mklement0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!