tl;dr
PS> "31222829" -replace '[0-9]', '$& '
3 1 2 2 2 8 2 9
Note that the output string has a trailing space, given that each digit in the input ([0-9]
) is replaced by itself ($&
) followed by a space.
As for what you tried:
"31222829" -replace ("[0-9]","$0 ")
While enclosing the two RHS operands in (...)
doesn't impede functionality, it's not really helpful to conceive of them as an array - just enumerate them with ,
, don't enclose them in (...)
.
Generally, use '...'
rather than "..."
for the RHS operands (the regex to match and the replacement operand), so as to prevent confusion between PowerShell's string expansion (interpolation) and what the -replace
operator ultimately sees.
Case in point: Due to use of "..."
in the replacement operand, PowerShell's string interpolation would actually expand $0
as a variable up front, which in the absence of a variable expands to the empty string - that is why you ultimately saw a blank string.
Even if you had used '...'
, however, $0
has no special meaning in the replacement operand; instead, you must use $&
to represent the matched string, as explained in this answer.
To unconditionally separate ALL characters with spaces:
Drew's helpful answer definitely works.
Here's a more PowerShell-idiomatic alternative:
PS> [char[]] '31222829' -join ' '
3 1 2 2 2 8 2 9
Casting a string to [char[]]
returns its characters as an array, which -join
then joins with a space as the separator.
Note: Since -join
only places the specified separator (' '
) between elements, the resulting string does not have a trailing space.