How can I get the last 4 digits in $MyBuildNumber, which could also be an asterisk? I want $NewVersion to return "2.8.1.*"; however, this code does not return anything for me:
$MyBuildNumber = "MyBuildNumberIs_2.8.1.*"
$VersionRegex = "\d+[*]?\.\d+[*]?\.\d+[*]?\.\d+[*]?"
$VersionData = [regex]::matches($MyBuildNumber,$VersionRegex)
switch($VersionData.Count)
{
0
{
Write-Error "Could not find version number data in MyBuildNumber."
exit 1
}
1 {}
}
$NewVersion = $VersionData[0]
Write-Host "Version: $NewVersion"
You can use character class with digit and *
:
[\d*]+\.[\d*]+\.[\d*]+\.[\d*]+
anubhava beat me to it, but here's a more compact version of the same thing because why not:
([\d*]+\.){3}[\d*]+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With