This is driving me nuts! Would appreciate any pointers as to what is going on, please!
$alpha = 'aaa.bbb.$connection.ccc.ddd'
$s = [regex]::match($alpha,"\$.+?(?=\.)").Value
"Alpha is: $alpha"
"Matched chunk is: $s"
$newChunk = "'" + $s + "'"
"New chunk is: $newChunk"
$beta = $alpha -replace $s,$newChunk
"Beta is: $beta"
This produces the following output:
Alpha is: aaa.bbb.$connection.ccc.ddd
Matched chunk is: $connection
New chunk is: '$connection'
Beta is: aaa.bbb.$connection.ccc.ddd
I am trying to identify/match character sequences in a path that begin with the literal '$' and continue until, but does not include a literal dot "." char. In the example code above the string would be "$connection".
Then I need to wrap that string in single quotes, so in the example above the $newChunk value would become '$connection' including the single quotes.
Next, I need to replace the matched value with the new single quoted value in the original string. No matter what I try (same with [regex]::match method too), the single quotes are stripped out. So, effectively, I am trying to turn:
aaa.bbb.$connection.ccc.ddd
into
aaa.bbb.'$connection'.ccc.ddd
Using Powershell 7.1 and would really appreciate someone telling me why this does not work. Thanks!