Forum Discussion
xjom71
Jan 19, 2021Copper Contributor
Impossible to use a capture as index?
I want to add a version to an Include in a csproj-file. (The variable $example contains only an Include-statement from a csproj-file)
I have a hashtable, with the name as a key, and an array of strings containing the version and the path.
So far, it seems that the capture, $1, is nulled, as soon as I try to use it to index the hashtable, $example_data.
It can be indexed with a variable, $example_var, containing exactly the same string as $1.
$1 can be found even inside the variable exapansion $(), but *not* in the indexing of the expanded hashtable.
Is it impossible, or am I doing things wrong here?
$example_data = @{Example = @("1.2.3", "bin")}
$example = '<None Include="Example" />'
$example_var = "Example"
# This works
$versioned_example = $example -replace 'Include="(.*)"', "Include=`"`$1`"-Version$($example_data.Get_Item("Example")[0])"
Write-Host $versioned_example
# This works
$versioned_example = $example -replace 'Include="(.*)"', "Include=`"`$1`"-Version$($example_data.Get_Item($example_var)[0])"
Write-Host $versioned_example
# This kind of works - at least we see that {$1} is actually "Example"
$versioned_example = $example -replace 'Include="(.*)"', "Include=`"`$1`"-Version$(({$1}))"
Write-Host $versioned_example
# But this does not work - why?
$versioned_example = $example -replace 'Include="(.*)"', "Include=`"`$1`"-Version$($example_data.Get_Item({$1})[0])"
Write-Host $versioned_example
# But this does not work - why?
$versioned_example = $example -replace 'Include="(.*)"', "Include=`"`$1`"-Version$($example_data.Get_Item("{$1}")[0])"
Write-Host $versioned_example
No RepliesBe the first to reply