简体   繁体   English

如何在PowerShell v2中匹配和替换Get-Content和Set-Content对象中的字符串?

[英]How to match and replace strings within Get-Content and Set-Content objects in PowerShell v2?

Fairly new to Powershell and have been working to replace outdated copyrights within an assembly file with updated copyrights. 这对Po​​wershell来说还很陌生,并且一直在努力用更新的版权替换程序集文件中的过时版权。 Using the function below however fills the entire Data with a repeat of my newCopyright text. 但是,使用下面的函数会用重复的newCopyright文本填充整个数据。

What am I doing wrong? 我究竟做错了什么?

function UpdateCopyright{
param            
(            
    [Parameter(Mandatory=$True,Position=1)]
    [String] $copyrightFileWithPath = "_"
)
try
{
    if ($copyrightFileWithPath -ne "_"){
        $currentYear         = (date).Year
        $copyrightNewTxt     = "Copyright © Company $currentYear"
        $copyrightSearchTxt  = "assembly: AssemblyCopyright"       
        $newCopyright = "[assembly: AssemblyCopyright(""$copyrightNewTxt"")]"
        $Data = (Get-Content $copyrightFileWithPath)          
        $copyrightInData = ($Data -match $copyrightSearchTxt)         
        $Data = ($Data -replace $copyrightInData, $newCopyright)
        Set-Content $copyrightFileWithPath $Data
        return $true
    }
    else{
        Write-Host ("ERROR: Invalid parameter to modify copyright for file " + $copyrightFileWithPath)
        return $false
    }
}
catch
{
    $ErrorMessage = $_.Exception.Message
    Write-Host ("ERROR: Exception while modifying copyright for file " + $copyrightFileWithPath + $ErrorMessage)
    return $false
}
}

Sample Input file: 样本输入文件:

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyProduct("Test App")]
[assembly: AssemblyCopyright("Copyright © Company 2014 - 2018")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("2.11.0.4")]
[assembly: AssemblyFileVersion("2.11.0.4")]

Sample Output file: 样本输出文件:

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyProduct("Test App")]
[assembly: AssemblyCopyright("Copyright © Company 2018")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("2.11.0.4")]
[assembly: AssemblyFileVersion("2.11.0.4")]

You perform a match with: 您与以下人进行比赛:

$copyrightInData = ($Data -match $copyrightSearchTxt)

That just returns an array with 1 item: the one line in the file that matches your pattern. 那只会返回一个包含1个项目的数组:文件中与您的模式匹配的一行。 Then you run: 然后,您运行:

$Data = ($Data -replace $copyrightInData, $newCopyright)

That looks at the whole file, looks for something that matches the pattern defined in $copywriteInData , and replaces it with the new copywrite text. 该命令将查看整个文件,查找与$copywriteInData定义的模式匹配的内容,并将其替换为新的copywrite文本。 Here's the thing, this is a Regular Expression (RegEx for short) match. 事情就是这样,这是一个正则表达式(简称RegEx)匹配。 So $copywriteInData is the string [assembly: AssemblyCopyright("Copyright © Company 2014 - 2018")] . 因此$copywriteInData是字符串[assembly: AssemblyCopyright("Copyright © Company 2014 - 2018")] In RegEx anything within square brackets means that it should match any character within, so when you do the -replace it looks at the first character and decides: 在RegEx中,方括号内的任何内容均应匹配其中的任何字符,因此在执行-replace它会查找第一个字符并决定:

Does it match: A
Does it match: s
Does it match: s
Does it match: e
Does it match: m

It proceeds until it finds a character within the [ ] that it matches, or runs out of characters within the [ ] to match against. 它继续进行,直到找到与之匹配的[ ]中的字符,或用尽[ ]的字符以进行匹配。 How to fix this is to properly escape your string for RegEx matching: 如何解决此问题是正确地将您的字符串转义为RegEx匹配:

$Data = $Data -replace [regex]::Escape($copyrightInData), $newCopyright

A simpler way to do this would be to better define what you want to replace, then just read the file, perform the replace, and write the file. 一种更简单的方法是更好地定义要替换的内容,然后仅读取文件,执行替换并写入文件。

    $currentYear         = (date).Year
    $copyrightSearchTxt  = '(?<=\[assembly: AssemblyCopyright\(").*?(?="\)])'
    (GC $copyrightFileWithPath) -replace $copyrightSearchTxt, $currentYear | Set-Content $copyrightFileWithPath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM