regex - Powershell 通配符/正则表达式替换

标签 regex powershell nuget

我正在尝试编写一个 PowerShell 脚本来更新我的 .nuspec文件(nuget 依赖)到最新的构建版本。不过,我在使用通配符时遇到了麻烦。

所以我想替换这一行的版本号

<dependency id="MyCompany.Common" version="1.0.0.0" />

到新的版本号,即版本 2.2.2.2
<dependency id="MyCompany.Common" version="2.2.2.2" />

我当前的放置方法如下所示。注意我需要一个通配符,因为我需要替换的解决方案中有多个 nuget 包,所有包都遵循 MyCompany.PackageName 的格式
$filecontent -replace 'id="MyCompany.*" version="*"', "$Version" | Out-File $file

但这实际上最终创造了
<dependency 2.2.2.21.0.0.0" />

如何修改我的正则表达式以确保它仅替换版本号组件?

最佳答案

用。。。来代替

$filecontent -replace 'id="MyCompany(.*?)" version=".*?"', "id=`"MyCompany`$1`" version=`"$Version`"" | Out-File $file

关于regex - Powershell 通配符/正则表达式替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42441110/

相关文章:

javascript - 用于仅匹配字符串中两个相同字符中的第一个的正则表达式

powershell - 谁见过这个潜在的 powershell 对象属性被视为方法调用陷阱?

powershell - Powershell中来自外部exe和我的自定义对象的输出

msbuild - 如何通过 MSBuild Copy 任务复制 NuGet 包引用的 contentFiles

visual-studio-2008 - JSON.NET Visual Studio 2008 和 .NET 3.5 Compact Framework

javascript - 如何将句子拆分为字母

python - 如何自定义使用 string.punctuation 过滤掉哪些字符?

javascript - 通过网站上的链接在服务器上运行 powershell 是个好主意吗?

c# - 在 vs 代码中连接到 Azure Devops 私有(private) NuGet

regex - 正则表达式在表达式之后是可选的,但不能包含特定字符(我在做什么错)?