I have a requirement where my string is of format as below:
<?define BuildNumber = "8314" ?>
I am using below powershell script in TFS 2017 build template to replace the build number value:
$content = Get-Content -path "$(Build.SourcesDirectory)\Install\Common\Constants.wxi"
$num = $(Build.BuildId)
$content -Replace '(BuildNumber\s*=\s*")\d*("\s*)', "`$1 $num `$2" | Out-File $(Build.SourcesDirectory)\Install\Common\Constants.wxi
This gives output like <?define BuildNumber = " 27994 " ?>
which is incorrect as I do not want spaces in the value.
When I tried using below code, it does not work.
$content -Replace '(BuildNumber\s*=\s*")\d*("\s*)', "`$1$num`$2" | Out-File $(Build.SourcesDirectory)\Install\Common\Constants.wxi
Output : <?define $27994 ?>
I tried all combinations but cannot get the quotations to work correctly. Please suggest a solution.
Use curly braces to "escape" group number
A little clarification about why the original code doesn't work: after $num variable has been resolved the replace string became $127994$2. That means that the -replace operator is trying to find group $127994 which obviously does not exist. When we add curly braces it becomes ${1}27994$2 which is completely legit.