I have this Select-String using a regex containing a named group
$m=Select-String -pattern '(?<mylabel>error \d*)' -InputObject 'Some text Error 5 some text'
Select-String does its job:
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:
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)
Select-Stringcan provide, use the vanilla-matchinstead. this'Some text Error 5 some text' -match '(?<mylabel>error \d*)'gives this$Matches.mylabelwith finally gives thisError 5.Get-ChildItem d:\logs\*.log -recurse | Select-String -pattern '<regex>'Groupsproperty. [grin] use this instead$m.Matches[0].Groups['mylabel'].$m.Matches[0]here while when using the numerical index of the regex group things works without that.$Collection.PropNamethe result can be a tad strange. [grin]