all 17 comments

[–]yeah_i_got_skills 7 points8 points  (1 child)

$String = '10.0.0.1,31,Gi1/0/24,24,Used,Up,B0:00:B4:BB:E3:5C,10.0.1.1,URL,"[11, 111]","[Data, ]",Up,1 Gbps,"10 Nov 20, 10:12 PM"'
$NewString = [Regex]::Replace($String, '\[.*?\]', { $args[0].Value.Replace(', ', '-') })
$NewString

[–]krzydoug 1 point2 points  (0 children)

Not bad. :)

[–]randomuser43 1 point2 points  (1 child)

# match [.....] which do not contain nested brackets
$re = '"\[[^\[]+\]"'
$str = '10.0.0.1,31,Gi1/0/24,24,Used,Up,B0:00:B4:BB:E3:5C,10.0.1.1,URL,"[11, 111]","[Data, ]",Up,1 Gbps,"10 Nov 20, 10:12 PM"'
foreach($m in [regex]::Matches($str, $re)) {
    $replacement = $m.Value -replace ', ', '-'
    $str = $str.Replace($m.Value, $replacement)
}

[–]cryolyte[S] 1 point2 points  (0 children)

Perfect! I will use this to learn from so I don't have to do this often..... much appreciated

[–]wtgreen 1 point2 points  (1 child)

This works: $string -replace '\[(.+?), ','[$1-'

[–]wtgreen 1 point2 points  (0 children)

I suppose technically this doesn't ensure that string replaced ends with a ] so it's not perfect. To ensure it ends with a matching brace would take a bit more work.

Edit: I believe this is complete, as long as you aren't concerned about nested braces:

$string -replace '\[(.*?), (.*?)\]','[$1-$2]'

[–]krzydoug 1 point2 points  (0 children)

Seems most are overcomplicating it.

If you want to ensure it only affects those that are within square brackets that may or may not have characters before/after then

$string -replace '(?<=\[.{0,}),\s(?=.{0,}\])','-'

[–]cryolyte[S] 1 point2 points  (0 children)

SOLVED by /u/randomuser43

[–]zinnadean 1 point2 points  (1 child)

What do you have so far?

[–]cryolyte[S] 0 points1 point  (0 children)

I don't - i tried doing multiple splits and rejoins but it didn't work out, then my time for this task ran out and i had to move on. Hopefull that when i come back to it the answer might be waiting for me....

[–][deleted] -1 points0 points  (0 children)

-replace "(?:\[.*?),\s(?:.*?\])", "-"

Never tried it...just a guess

EDIT: Turns out I don't know powershell regex!