powershell无法对反向引用匹配使用方法
我试图在运行中更努力地转换案例,但powershell方法似乎不适用于下面的backrefence匹配示例:powershell无法对反向引用匹配使用方法,powershell,backreference,Powershell,Backreference,我试图在运行中更努力地转换案例,但powershell方法似乎不适用于下面的backrefence匹配示例: $a="string" [regex]::replace( "$a",'(.)(.)',"$('$1'.toupper())$('$2'.tolower())" ) > string $a -replace '(.)(.)',"$('${1}'.toupper())$('${2}'.tolower())"
$a="string"
[regex]::replace( "$a",'(.)(.)',"$('$1'.toupper())$('$2'.tolower())" )
> string
$a -replace '(.)(.)',"$('${1}'.toupper())$('${2}'.tolower())"
> string
expected result
> StRiNg
不知道是否可能您需要一个脚本块来调用String类方法。你可以有效地做你想做的事。对于Windows PowerShell,您不能使用
-replace
运算符进行脚本块替换。您可以在PowerShell Core(v6+)中执行此操作,不过:
请注意,脚本块替换可识别当前的MatchInfo
对象($\uuu
)。使用Replace()
方法,脚本块作为自动变量$args
中的参数传递到MatchInfo
对象中,除非指定param()
块
# Windows PowerShell
$a="string"
[regex]::Replace($a,'(.)(.)',{$args[0].Groups[1].Value.ToUpper()+$args[0].Groups[2].Value.ToLower()})
# PowerShell Core
$a="string"
$a -replace '(.)(.)',{$_.Groups[1].Value.ToUpper()+$_.Groups[2].Value.ToLower()}