【发布时间】:2015-11-13 14:50:25
【问题描述】:
我很难找到一个“本机”powershell -replace 调用,它将用基于匹配的东西替换匹配。例如。在 perl 中是这样的:
my %definedVars = ( ab => "hello", cd => "there" );
my $str = q"$(ab) $(cd)";
$str =~ s/\$\(([^)]+)\)/$definedVars{$1}/ge;
print "$str\n";
我在想,在 powershell 中,它会是这样的:
$definedVars = @{ ab = 'hello'; cd = 'there' }
'$(ab) $(cd)' -replace "\$\(([^)]+)\)", { $definedVars[$1] }
我环顾四周,我认为 -replace 开关没有延迟评估器,所以我必须使用 .NET replace 函数,但我不完全确定它是如何工作的.我想应该是这样的:
$definedVars = @{ ab = 'hello'; cd = 'there' }
[regex]::replace('$(ab) $(cd)',
"\$\(([^)]+)\)",
{ $definedVars[$_.Groups[1].Value] } )
文档很少,所以如果你也能说明你从哪里得到你的信息,那就太好了。
【问题讨论】:
标签: regex perl powershell