使用正则表达式 + 替换匹配组电源外壳
我正在尝试设置一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
亚伯拉罕的有用评论已经指出了关键问题,
.Replace(..)
字符串方法 与正则表达式不兼容,您可以使用-replace
运算符正则表达式替换。至于您想要的输出,您可以使用以下模式:请参阅 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: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.