The powershell script is as follows:
$verNumber ="123"
# Replace last digit of AssemblyVersion
$assemblyInfo = $assemblyInfo -replace "^\[assembly: AssemblyVersion\(`"([0-9] )\.
([0-9] )\.([0-9] )\.[0-9] `"\)]",
('[assembly: AssemblyVersion("$1.$2.$3.' $verNumber '")]')
# Replace last digit of AssemblyFileVersion
$assemblyInfo = $assemblyInfo -replace
"^\[assembly: AssemblyFileVersion\(`"([0-9] )\.([0-9] )\.([0-9] )\.[0-9] `"\)]",
('[assembly: AssemblyFileVersion("$1.$2.$3.' $verNumber '")]')
This is what is in my file.
// [assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyVersion("1.1.0.123")]
[assembly: AssemblyFileVersion("1.1.0.123")]
line 2 and 3 get updated but I am trying to update 1 , How can i get that pattern in the regex ?
CodePudding user response:
This should be a simplified version of the regex you wanted to accomplish. See this regex101 link for details.
$verNumber = 123
$assemblyinfo = @'
// [assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyVersion("1.1.0.123")]
[assembly: AssemblyFileVersion("1.1.0.123")]
'@
$assemblyinfo -replace '(?<=(^//\s*\[assembly: AssemblyVersion\("(\d \.){3}))\d ', $verNumber
CodePudding user response:
You could do all 3 replacements using 2 capture groups in a single replacement and an optional part in the pattern for the leading slashes and another optional part for the File
(?m)^((?:// )?\[assembly: Assembly(?:File)?Version\("(?:\d \.){3})[0-9] ("\)])$
The pattern matches:
(?m)
Inline modifier for multiline^
Start of string(
Capture group 1(?:// )?
Optionally match//
\[assembly: Assembly(?:File)?Version
Match[assembly: AssemblyVersion
or[assembly: AssemblyFileVersion
\("
Match("
(?:\d \.){3}
Match 3 repetitions of 1 digits 0-9 and a dot)
Close group 1[0-9]
Match 1 digits that should be replaced("\)])
Capture group 2, match)]
$
End of string
For example, reading a file.txt that contains the example strings:
$verNumber ="123"
$assemblyInfo = Get-Content -Raw file.txt
$assemblyInfo = $assemblyInfo -replace "(?m)^((?:// )?\[assembly: Assembly(?:File)?Version\(`"(?:\d \.){3})[0-9] (`"\)])$", "`${1}$verNumber`${2}"
Write-Output $assemblyInfo
Output
// [assembly: AssemblyVersion("1.1.0.123")]
[assembly: AssemblyVersion("1.1.0.123")]
[assembly: AssemblyFileVersion("1.1.0.123")]