3/31/10

Insert String Every N Characters

This PowerShell v2 script inserts a string every N characters. The interval is specified using the regex repetition operator, {min,max}.
$a = 'abcdefghijklmnopqrstuvwxyz'
([regex]::matches($a, '.{1,3}') | %{$_.value}) -join ' '
([regex]::matches($a, '.{1,8}') | %{$_.value}) -join '..'
([regex]::matches($a, '.{1}') | %{$_.value}) -join '-'

#output
abc def ghi jkl mno pqr stu vwx yz
abcdefgh..ijklmnop..qrstuvwx..yz
a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z

No comments:

Post a Comment