2016-02-05 23 views
0

我试图改变一个文件(数千文件,事实上)使用这样的东西的情况下...我怎样才能改变大小写替换变量的替换变量使用powershell

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

我不知道如何获得替代变量,$ 1是小写(或更好,但适当的情况下)

+0

的可能的复制[在PowerShell中使用函数代替](http://stackoverflow.com/questions/30666101/use-a-function-in- PowerShell的替换) – PetSerAl

回答

0

-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()}) 
    } 
}