我试图在字符串中找到regex模式,并使用哈希表替换它。第一个示例工作得很好,我得到的结果是"$$oldstring$$ $$oldstring$$“。我不知道如何将'$1‘作为键传递到哈希表中,以便将其替换为与我的散列中的键对应的值。
这是我的代码:
$hashtable = @{'$$oldstring$$' = 'newstring'}
$testString = '$$oldstring$$'
$replaced = [regex]::Replace($testString, '(\$\$(.*?)\$\$)', '$1 $1')
$replaced
$replaced2 = [regex]::Replace($testString, '(\$\$(.*?)\$\$)', $hashtable.Get_Item($1))
$replaced2以及产出:
$$oldstring$$ $$oldstring$$
Exception calling "get_Item" with "1" argument(s): "Key cannot be null.
Parameter name: key"我知道$hashtable.Get_Item( $1 )不是有效的语法,$1在这里是空的,但似乎无法找到正确的方法。
发布于 2015-09-24 01:18:37
好像是我自己想出来的,不得不用这样的脚本块:
$replaced2 = [regex]::Replace($testString, '(\$\$(.*?)\$\$)', { $hashtable.Get_Item($args[0].Value) } )更多信息在这里:Evaluator
https://stackoverflow.com/questions/32751722
复制相似问题