2016-02-05 24 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()}) 
    } 
}