I want to replace octal codes with the ascii character equivalents in a string.
Input example: \052.test.com
I've tried all sorts of different combinations, but why doesn't this work, and how do I fix it?
> "\052.test.com" -replace '\\(0[1-7][0-7])', "`$1"
052.test.com
> "\052.test.com" -replace '\\(0[1-7][0-7])', [char][convert]::ToInt32("052", 8)
*.test.com
> "\052.test.com" -replace '\\(0[1-7][0-7])', [char][convert]::ToInt32("`$1", 8)
MethodInvocationException: Exception calling "ToInt32" with "2" argument(s): "Could not find any recognizable digits."
In Windows PowerShell, the second right-hand argument to
-replacewill be evaluated like any other expression, and the resulting argument value treated as a string no matter what you pass to it.To invoke a callback function against each match, you can use
[regex]::Replace(...), and pass a scriptblock in place of the substitution string - the runtime will pass aMatchInfoobject as the first argument to the scriptblock, from where you can extract the capture groups value:In PowerShell 6.2+ and 7 you can pass the scriptblock directly as an operand to
-replace- theMatchInforesult will be bound to$_automatically: