How to match a string array with a string variable?

I have an string array

$list = @("abc-1.0.1-xyz", "abc-1.0.2-xyz", "abc-1.0.3-xyz", "abc-1.0.4-xyz")

and a variable declared as $v = '1.0.2'.

Now I want to compare my array with the variable and get all the strings containing value greater than $v in another array.

For example: in this case abc-1.0.3-xyz and abc-1.0.4-xyz (greater than $v i.e. 1.0.2) will be added to another array.

1 answer

  • answered 2018-01-17 07:10 PaweĊ‚ Dyl

    I would use simple regex:

    $list = 'abc-1.0.1-xyz', 'abc-1.0.2-xyz', 'abc-1.0.3-xyz', 'abc-1.0.4-xyz'
    $v = '1.0.2'
    $list | % {
        $match = [regex]::Match($_, '\d+\.\d+\.\d+').Value
        if ($match -gt $v) { $_ }



    If you wish to use version comparison (string rules differs, i.e. 10<9), replace condition with following:

    if ([version]$match -gt [version]$v) { $_ }