As a part of a buildscript in powershell, I am trying to extract a version number from a string using regular expression. The number is assumed to be in a format xx.yy (eg. 10.6) I need the Integer part (in this example, it would be 10) and the fraction part (example: 6)
I want to have a method that checks that both my patterns exists and thereafter extracts the numbers. (I am very much a novice in powershell, and am trying to avoid using C# functions, but rather powershell itself.)
I tried to do this:
$integralPart="A"
$fractionPart="B"
function GetVersion {
param([string]$strVersion)
$integralPattern="^[0-9]+(?=\.)"
$fractionalPattern="(?<=\.)[0-9]+$"
#Check if string consists of an integral and fractional part
If ($strVersion -match $integralPattern -eq $True -and $strVersion -match $fractionalPattern -eq $True)
{
$strVersion -match $integralPattern
$integralPart = $matches[0]
$strVersion -match $fractionalPattern
$fractionalPart = $matches[0]
}
else
{
Write-Host "You did not enter a double with an integer and a fractional part (eg. 10.6)"
Exit
}
}
GetVersion (Read-Host 'Enter program version')
Write-Host $integralPart
Write-Host $fractionPart
In doing so, I was hoping that $integralPart and $fractionalPart would contain my numbers, at they match the values they should
Can anyone explain how this can be done?