替换管道

时间:2016-09-29 09:24:12

标签: powershell

我使用以下脚本删除git merged分支:

function Remove-MergedBranches
{
  git branch -a --merged |
    ForEach-Object { $_.Trim() } |
    Where-Object {$_ -NotMatch "^\*"} |
    Where-Object {-not ( $_ -Like "*master" )} |
    Where-Object {-not ( $_ -Like "*develop" )} |
    Where-Object {-not ( $_ -Like "*dev" )} |
    % {$_.replace("/remotes","")} |
    ForEach-Object { git branch -d $_ }
}

replace无效。

分支可能是remotes/origin/tg-training-section

1 个答案:

答案 0 :(得分:3)

你要替换/remotes中不匹配的remotes/origin/tg-training-section(它不是以斜杠开头),所以你可能想要省略斜杠:

# ....
% {$_.replace("remotes","")}

另一种方法是使用正则表达式/remotes替换remotes-replace

# ....
% {$_ -replace '\/?remotes'}

我还会考虑使用\b\/?remotes来确保您不会替换字符串中的remotes