You can use a combination of -split
, -replace
and -join
like this:
$iban = 'HU42 1177 3016 1111 1018 0000 0000'
(($iban -split '\s', 2)[-1] -replace '\s') -split '(\d{8})' -ne '' -join '-'
Result:
11773016-11111018-00000000
I see from your comment you also figured out a way of doing this, by:
- First place the "-" characters at the correct positions
- Then Remove first 5 characters ("HU42 ")
- Finally remove remaining space characters
("HU42 1177 3016 1111 1018 0000 0000" -replace "(^.{14})\s(.{9})\s(.{9})",'$1-$2-$3').Substring(5) -replace " ",""
Remember the $1-$2-$3
need to be in single-quotes