I'm sure this is on here somewhere, but it did not appear in the top 100 search results. It's hard to search for.
But basically The [regex]::Matches() function returns a MatchCollection type object which, let's say, is very "array-like".
However it does not support the convenient [-1] array index, indeed, nor any negative array indices.
That is surprising to me.
This is true even tho the MatchCollection type implements the ICollection and IEnumerable interfaces. (I confess I don't know where [-1] comes from. Apparently not these.)
Here's some code that demonstrates:
'MatchCollection type object returned from [regex]::Matches() does not support [-1] array index.'
'for comparison, a regular array:'
(1,2,3)[-1]
# returns 3
''
'the [regex]::Matches() function:'
$match_info_obj = [regex]::Matches('abc', '.')
$match_info_obj.Count
# returns 3
$match_info_obj.Value
# returns:
# a
# b
# c
'MatchCollection type which implements ICollection and IEnumerable does not support the [-1] array index:'
$match_info_obj.GetType() | select fullname, ImplementedInterfaces | fl
# returns:
# FullName : System.Text.RegularExpressions.MatchCollection
# ImplementedInterfaces : {System.Collections.ICollection, System.Collections.IEnumerable}
$match_info_obj[-1] -eq $null
# returns True. Ie, it's null.
$match_info_obj[$match_info_obj.Count - 1].Value
# returns the expected value: 'c'
So am I missing something? Or is this just an unfortunate limitation in powershell?