Powershell: add Index and LastIndex property to $matches variable

Created on 2 Feb 2020  路  7Comments  路  Source: PowerShell/PowerShell

steps to reproduce

PS C:\> 'hello' -match 'e'
True
PS C:\> $Matches | fl *

Name      : 0
Key       : 0
Value     : e
Index     : 1
LastIndex : 2
Issue-Enhancement Resolution-Duplicate

All 7 comments

I'm not really sure what you're asking here. What purpose would this serve? $matches is just a hashtable; all it really has is key and value for each item.

I'd also recommend you follow the feature suggestion template we have so that others can more easily understand what exactly it is that you're proposing 馃槉

It is about first and last position of found substring in source line.

@iSazonov thanks, that makes a bit more sense! It does raise the question of whether we want First/LastIndex of just the 0 entry, though, or whether we'd want to add it for each match group entry as well?

I guess it is based on Regex and we can not add the indexes without performance degradation.

Adding new entries to $Matches is also a potentially breaking change.

However, we discussed returning [System.Text.RegularExpressions.Match] instances in the context of a future -allmatches operator - #7867 - which would at least enable you to do:

PS> ('hello' -allmatches 'e')[0]

Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 1
Length   : 1
Value    : e

@mklement0 Can you move this in #7867 explicitly?

Assuming you meant my previous comment, @iSazonov: done.

Was this page helpful?
0 / 5 - 0 ratings