【问题标题】:How can I change the case of a substitution variable in a -replace using powershell如何使用 powershell 在 -replace 中更改替换变量的大小写
【发布时间】:2016-02-05 09:54:22
【问题描述】:

我正在尝试使用类似这样的方式更改文件(实际上是数千个文件)中某事物的大小写...

ls *.pro | ForEach-Object {
    (Get-Content -Path $_.FullName) -replace "sender_name\(\s*``([^``]+)``\s*\)", 'sender_name( `$1` )'
}

我不知道如何获取替换变量,$1 是小写(或者更好的是,正确的大小写)

【问题讨论】:

标签: powershell


【解决方案1】:

-replace 运算符不支持对替换字符串进行花哨的后处理。您必须在每一行上使用Regex.Replace()

# Define your regex matcher
$Regex = [regex]'sender_name\(\s*`([^`]+)`\s*\)'

Get-ChildItem *.pro |ForEach-Object {
    Get-Content $_.FullName |ForEach-Object {
        # Replace using a match evaluator
        $Regex.Replace($_,{param($MatchInfo) $MatchInfo.Groups[1].Value.ToLower()})
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 2023-03-20
    • 2015-06-24
    相关资源
    最近更新 更多