Question:
I have this Select-String using a regex containing a named group
1 2 |
$m=Select-String -pattern '(? |
Select-String does its job:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
PS > $m.Matches.groups Groups : {0, mylabel} Success : True Name : 0 Captures : {0} Index : 10 Length : 7 Value : Error 5 Success : True Name : mylabel Captures : {mylabel} Index : 10 Length : 7 Value : Error 5 |
I can get the value of the matching named group by using the index of the group, no problem:
1 2 3 |
PS > $m.Matches.groups[1].Value Error 5 |
But I have no success in getting the same result by using the named regex group (mylabel). I found statements like $m.Matches.groups["mylabel"].Value
but that doesn’t work on my machines (W10/W2012, PS 5.1)
Answer:
You got one correct answer in the comment above, but here is how to do it without using the 0 match index:
1 2 |
$m.Matches.groups | ? { $_.Name -eq 'mylabel' } | Select-Object -ExpandProperty Value |