powershell - 如何在powershell中[int]添加[string]正则表达式替换?

标签 powershell replace callback delegates

我正在使用正则表达式替换来重命名文件:

ls *dat | rename-item -newname {$_.name -replace '(.*)_([0-9]+)(.dat)','$1($2)$3'}

但我还想向 [string]$2 添加一个 constant int ;类似的东西:

'$1(@{int.parse($2)+3})($3)'

我应该如何进行?

最佳答案

PowerShell Core v6.1.0+ 支持传递脚本 block 作为
的替换操作数 -替换运算符
;为找到的每个匹配项调用脚本 block ,其输出形成替换字符串,从而启用算法替换(而不仅仅是文本替换) :

'foo_10.dat' -replace '(.*)_([0-9]+)(\.dat)', { 
  '{0}{1}{2}' -f $_.Groups[1].Value, 
                 ([int] $_.Groups[2].Value + 3), 
                 $_.Groups[3].Value
}

以上产量:

foo13.dat

请注意如何将 3 添加到 10(并且删除了下划线):

脚本 block 内的

$_[System.Text.RegularExpressions.Match]委托(delegate)比赛结果的实例;例如,$_.Value 表示完全匹配,而 $_.Groups[1].Value 表示第一个捕获组匹配的内容。

该功能基于 [regex] .NET type's .Replace() method 构建,它也可以在早期的 PowerShell 版本中直接使用(但不太容易) - 见下文。


Windows PowerShell中,您有两个选项:

  • 首先使用 -match 运算符,然后根据自动 $Matches 变量中反射(reflect)的匹配信息在单独的语句中执行转换,如建议的通过 kuujinbo :

    $null = 'foo_10.dat' -match '(.*)_([0-9]+)(\.dat)' # match first
    '{0}{1}{2}' -f $Matches.1, ([int] $Matches.2 + 3), $Matches.3 # build via $Matches
    
  • 直接使用 .NET 框架,并将脚本 block 作为委托(delegate)(回调函数)传递给上述[regex]::Replace() 方法,如 Ansgar Wiechers 建议:

    ([regex] '(.*)_([0-9]+)(\.dat)').Replace('foo_10.dat', {
      param($match)
      '{0}{1}{2}' -f $match.Groups[1].Value, ([int] $match.Groups[2].Value + 3), $match.Groups[3].Value
    })
    
    • 请注意,必须在此处声明形式参数 - param($match) 才能访问匹配结果,而顶部的纯 PowerShell 解决方案能够使用 $ _,像往常一样。

关于powershell - 如何在powershell中[int]添加[string]正则表达式替换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51505651/

相关文章:

javascript - 按顺序执行导入的 Javascript 函数

ruby-on-rails - 在 Rails around_create 回调中使用事务

.net - 如何调试/注册在文件创建时运行脚本的永久 WMI 事件

c# - 为什么不能在 C# 中将 Powershell 作为常规进程调用

Linux Sed 在替换期间添加新行

c - 如何在不使用 <string.h> 的情况下将字符串与子字符串进行比较以将第一个和第二个之间的相等部分转换为 '*'

javascript - 将回调添加到 JavaScript 函数中(AngularJS 上的应用)

arrays - 使用 powershell 的数组中的 "Counting"

powershell - 用增量值替换字符

arrays - 用 nan 替换缺失值