To complement Mathias' helpful answer with a concise alternative based on the -replace
operator:
# PowerShell [Core] only (v6.2+) - see bottom for Windows PowerShell solution.
PS> '0.0.3', '1.123.3' -replace '(?<=\.)[^.]+$', { 1 + $_.Value }
0.0.4
1.123.4
Regex (?<=\.)[^.]+$
matches the last component of the version number (without including the preceding .
in the match).
Script block { 1 + $_.Value }
replaces that component with its value incremented by 1.
For solutions to incrementing any of the version-number components, including proper handling of [semver]
version numbers, see this answer.
In Windows PowerShell, where the script-block-based -replace
syntax isn't supported, the solution is more cumbersome, because it requires direct use of the .NET System.Text.RegularExpressions.Regex
type:
PS> '0.0.3', '1.123.3' | foreach {
[regex]::Replace($_, '(?<=\.)[^.]+$', { param($m) 1 + $m.Value })
}
0.0.4
1.123.4