0

I would like to convert the following iban in powershell with -replace

from

HU42 1177 3016 1111 1018 0000 0000

to

11773016-11111018-00000000

so how could i write a correct pattern to replace the 3rd space and 2nd after that? Then remove every remaining space

mklement0
  • 382,024
  • 64
  • 607
  • 775

5 Answers5

4

Another alternative

"{1}{2}-{3}{4}-{5}{6}" -f -split 'HU42 1177 3016 1111 1018 0000 0000'

Output

11773016-11111018-00000000
Doug Maurer
  • 8,090
  • 3
  • 12
  • 13
3
$Old = "HU42 1177 3016 1111 1018 0000 0000"
$SplitOld = $old.Split(" ")
$New = $SplitOld[1] + $SplitOld[2]+ "-" + $SplitOld[3] + $SplitOld[4] + "-" + $SplitOld[5] + $SplitOld[6]

or on one line:

$Old = "HU42 1177 3016 1111 1018 0000 0000"
$New = $old.Split(" ")[1] + $old.Split(" ")[2]+ "-" + $old.Split(" ")[3] + $old.Split(" ")[4] + "-" + $old.Split(" ")[5] + $old.Split(" ")[6]
Judd Davey
  • 349
  • 2
  • 5
3

You can use a two-step -replace approach that is conceptually reasonably simple:

'HU42 1177 3016 1111 1018 0000 0000' -replace '^.+? | ' -replace '(\d{8})(?!$)', '$1-'
  • ^.+? | removes the first space-separated token and the space that follows it, as well as all remaining spaces.

  • (\d{8})(?!$) matches all 8-digit groups except the one at the end of the string ($, via a negative look-ahead assertion, (?!...)), and replaces each with itself ($1, the first capture group) followed by -, thereby effectively inserting -.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • negative look-ahead meen only match if next up instance would not match with negative-look ahead statement? – Norbert Pozsonyi Aug 20 '21 at 14:39
  • @NorbertPozsonyi, if I understand you correctly, yes. The subexpression preceding the negative look-ahead matches only if the negative look-ahead does _not_ match. Here it is used to exclude the _last_ 8 digits from being matched. See [Zero-Width Negative Lookahead assertions](https://learn.microsoft.com/en-us/dotnet/standard/base-types/grouping-constructs-in-regular-expressions#zerowidth_negative_lookahead_assertion) – mklement0 Aug 20 '21 at 14:43
2

Do you really need to use regex to solve your problem?

$iban = 'HU42 1177 3016 1111 1018 0000 0000'
$splitted = -split $iban
(1,3,5 | ForEach-Object { -join $splitted[$_++, $_] }) -join '-'

Try it online!

The problem is more and more like Code Golf :)

$iban = 'HU42 1177 3016 1111 1018 0000 0000 1704 2345'
$l = 0
-join (
  -split $iban |
  ForEach-Object {
    if( $l -eq 0 ) {
      $r, $l = '', 8
    } else {
      $d = ''
      if( $r.length -eq $l ) {
        $l += 9
        $d = '-'
      }
      $r = "$r$d$_"
    }
  }
)
$r

Try it online!

Andrei Odegov
  • 2,925
  • 2
  • 15
  • 21
2

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

Theo
  • 57,719
  • 8
  • 24
  • 41
  • Why not work this? First place the "-" Than Remove first 4 chair Than remove remaining space? ("HU42 1177 3016 1111 1018 0000 0000" -replace "(^.{14})\s(.{9})\s(.{9})","$1 -$2-$3").remove(0,5) -replace " ","" – Norbert Pozsonyi Aug 20 '21 at 14:28
  • 2
    @NorbertPozsonyi Well... because as you can see, there are lots of alternative ways to do this. If you change your code to `("HU42 1177 3016 1111 1018 0000 0000" -replace "(^.{14})\s(.{9})\s(.{9})",'$1-$2-$3').Substring(5) -replace " ",""` it will work too. – Theo Aug 20 '21 at 14:41
  • Somehow i didn't know that it should '...' instead of "..." – Norbert Pozsonyi Aug 20 '21 at 16:40