使用正则表达式 + 替换匹配组电源外壳

发布于 2025-01-12 16:25:12 字数 1856 浏览 0 评论 0原文

我正在尝试设置一个 PowerShell 脚本来替换 GlobalAssemblyInfo.cs 文件中的匹配字符串。我基本上希望通过 powershell 更新版本号,这样我就可以在构建后自动执行此操作。无论如何,cs 文件中的输入字符串是这样的:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Runtime.InteropServices;


[assembly: System.Reflection.AssemblyVersion("1.1.31.0")]
[assembly: System.Reflection.AssemblyCompany("Name")]
[assembly: System.Reflection.AssemblyProduct("Name")]
[assembly: System.Reflection.AssemblyCopyright("Name")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

我正在尝试的 PowerShell 脚本是这样的:

$file = "C:\users\konrad\desktop\GlobalAssemblyInfo.cs"
$pattern = '^\[assembly: System\.Reflection\.AssemblyVersion\("(.*)"\)\]'
$version = '2.0.0.0_delta'

(Get-Content $file).replace($pattern, "$1 $version") | Set-Content $file

再次,想法是获取 ("") 之间的版本号并替换它带有一个名为 $version 的字符串。上面的代码我没有收到任何错误。这根本行不通。任何想法将不胜感激。

干杯!

编辑:

@Abraham Zinala 建议使用 -replace 代替。我试过了。

(Get-Content $file) - 替换 $pattern, "$1$version" | Set-Content $file

[ assembly: System.Reflection.AssemblyVersion("1.1.31.0")]

替换为:

2.0.0.0_delta

我想要的是这样的:

[程序集:System.Reflection.AssemblyVersion("2.0.0.0_delta")]

I am trying to setup a PowerShell script that would replace a matching string from my GlobalAssemblyInfo.cs file. I am basically looking to update the Version number via powershell so I can do that in a post build automatically. Anyways, the input string from the cs file is this:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Runtime.InteropServices;


[assembly: System.Reflection.AssemblyVersion("1.1.31.0")]
[assembly: System.Reflection.AssemblyCompany("Name")]
[assembly: System.Reflection.AssemblyProduct("Name")]
[assembly: System.Reflection.AssemblyCopyright("Name")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

My PowerShell script that I am attempting is this:

$file = "C:\users\konrad\desktop\GlobalAssemblyInfo.cs"
$pattern = '^\[assembly: System\.Reflection\.AssemblyVersion\("(.*)"\)\]'
$version = '2.0.0.0_delta'

(Get-Content $file).replace($pattern, "$1 $version") | Set-Content $file

Again, the idea is to get the version number that is between the ("") and replace it with a string called $version. I am not getting any errors with the above code. It simply doesn't work. Any ideas would be appreciated.

Cheers!

EDIT:

@Abraham Zinala suggested using -replace instead. I tried that.

(Get-Content $file) -replace $pattern, "$1$version" | Set-Content $file

This replaces this:

[assembly: System.Reflection.AssemblyVersion("1.1.31.0")]

into this:

2.0.0.0_delta

What I want is this:

[assembly: System.Reflection.AssemblyVersion("2.0.0.0_delta")]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没有你 2025-01-19 16:25:12

亚伯拉罕的有用评论已经指出了关键问题,.Replace(..) 字符串方法 与正则表达式不兼容,您可以使用 -replace 运算符正则表达式替换。至于您想要的输出,您可以使用以下模式:

$pattern = '(?m)(^\[assembly: System\.Reflection\.AssemblyVersion\(")[\d.]+'
$version = '2.0.0.0_delta'

(Get-Content $file -Raw) -replace $pattern, "`${1}$version" | Set-Content ...

请参阅 https://regex101.com/r/VLoUN2 /1 进行解释。

您可能需要将 [\d.]+ 更改为 [\w\d.]+,以防万一您要替换的版本还可能包含单词字符和下划线

Abraham's helpful comment already pointed out the key issue, .Replace(..) string method is not regex compatible, you can use the -replace operator for regex replacements. As for your desired output, you could use the following pattern:

$pattern = '(?m)(^\[assembly: System\.Reflection\.AssemblyVersion\(")[\d.]+'
$version = '2.0.0.0_delta'

(Get-Content $file -Raw) -replace $pattern, "`${1}$version" | Set-Content ...

See https://regex101.com/r/VLoUN2/1 for explanation.

You might want to change [\d.]+ for [\w\d.]+ in case the version you're looking to replace could also contain word characters and underscores.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文