powershell - powershell 不能对反向引用匹配使用方法
问题描述
我更努力地即时转换大小写,但似乎 powershell 方法不适用于下面的反向引用匹配示例:
$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+) 中执行此操作:
# 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()}
请注意,脚本块替换识别当前MatchInfo
对象 ( $_
)。使用该方法,除非您指定一个块,否则Replace()
脚本块将MatchInfo
作为自动变量中的参数传入对象。$args
param()
推荐阅读
- c# - 如何使用存储过程从数据库中获取数据?
- intellij-idea - 如何在 IntelliJ IDEA 中再次使项目树布局紧凑?
- c++ - 为什么 STL 容器适配器堆栈中的顶部返回一个 const 引用?
- kiwi-tcms - 提交/构建数据应该存储在哪里?
- excel - 授予对具有特定用户 Windows 凭据的图书的访问权限
- mongodb - 如何使用 Mongoose 找到存储在另一个数组中的数组?
- java - Junit 测试单独通过,而不是分组通过
- git - Git - 不同版本之间的合并
- entity-framework - 当有多个包含时,从 ef core 2.2.6 迁移到 3.1 会出现问题
- javascript - 尝试覆盖现有 POST 后如何修复 401?