I am trying to convert some string input into the correct format for working with a MAC address.
So I need to convert
00A0C914C829
to
00:A0:C9:14:C8:29
I have this PowerShell
script to achieve this:
$string = "00A0C914C829"
$out = $string -replace "([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])", '$1$2:$3$4:$5$6:$7$8:$9$10:$11$12'
$out
Which outputs:
00:A0:C9:14:C8:29
But that regex seems ridiculously long.
Is there some way of making this more concise?
- I think I need to group each section into variables using
()
, which is why it is so long. I also tried matching with^([0-9A-Fa-f]){12}$
, but this gave the output9$2:$3$4:$5$6:$7$8:$9$10:$11$12
(12 groups of 1 character each) - I also tried the similar regex
^(([0-9A-Fa-f]){2}){6}$
(6 groups of 2 characters each) and then the replacement:$1:$2:$3:$4:$5:$6
, but this gave output29:9:$3:$4:$5:$6
I think the problem is that I don't understand how groups work properly ... Would really appreciate if someone could point me in the right direction!