S
S
Sergey K2018-10-26 20:58:19
PowerShell
Sergey K, 2018-10-26 20:58:19

How to replace a substring with a value from a Dictionary?

The bottom line is this: you need to replace all occurrences of the @some_text type with a value from the dictionary
. There is such a script:

[string]$str = "-Password '@Password' -Branches '@BranchList'"

[System.Collections.Specialized.OrderedDictionary]$ValuesForParameters = @{}
$ValuesForParameters.Add("BranchList", "Replaced BranchList")
$ValuesForParameters.Add("Password", "Replaced Password")

$regularExpression = "(?<='@)(.*?)(?=')"
[string]$result = $str -replace $regularExpression, $ValuesForParameters['$1']
$ValuesForParameters.Keys
Write-Host $result

But the result outputs:
-Password '@' -Branches '@'
As I understand it, it does not perceive '$1' as a variable, but perceives it as a string, if I add a value with the key '$1' to the dictionary, it will replace everything with the value corresponding to this key.
Could you suggest how this can be implemented?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ApeCoder, 2018-10-26
@AceLightning

https://stackoverflow.com/questions/10995667/lambd...
You can use the -, delegate parameter from regex.replace

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question