【发布时间】:2016-12-15 07:45:53
【问题描述】:
我有一个替换的 PowerShell 脚本
"version" : "xxx"
与
"version" : "myBuildNumber"
现在我发现我的文件中有多个这样的文件。 我只想替换第一个匹配项。
我已经尝试过Powershell - Replace first occurrences of String,但它不适用于我的正则表达式。
这是我的脚本:
(Get-Content myFile.txt) -replace '(?<pre>"version"[\s]*:[\s]*)(?<V>"[^\"]*")', "`$1`"$Env:BUILD_VERSION`"" | Out-File myFile.txt
【问题讨论】:
-
何乐而不为,同样的方法,只是使用一个DOTALL修饰符:
(Get-Content myFile.txt) -replace '(?s)(?<pre>"version"\s*:\s*)(?<V>"[^"]*")(.*)', "`$1`"$Env:BUILD_VERSION`"`$3" | Out-File myFile.txt -
你尝试修改一个json吗?
-
是的,来自 dotnet core 的 project.json
标签: json regex powershell powershell-2.0 powershell-3.0