All PowerShell Versions
Ever wanted to split a string in chunks of a given length? Regular expressions can help. Here is an example that splits a list of numbers in groups of 8 digits each:
'000111010010010000101001' -split '(\d{8})'
It works, but there are empty results emitted as well. The correct way is to use look-aheads and look-behinds:
'000111010010010000101001' -split '(?<=\G\d{8})(?=.)'
Basically, you are telling –split that you would like to split after 8 digits (\d{8}), but only if there are still characters following (?=.), and you want the match to continue after the previous match (\G).
While you might not have composed this regular expression yourself, you can now use it as a very useful template. Just adjust the group size and the placeholder. Tomorrow, we’ll look further into this.