7

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)

6
  • unless you have a need for something special that only Select-String can provide, use the vanilla -match instead. this 'Some text Error 5 some text' -match '(?<mylabel>error \d*)' gives this $Matches.mylabel with finally gives this Error 5. Commented Feb 22, 2019 at 10:49
  • @Lee_Dailey I have a reason to use Select-String. My real world script searches potentially hundreds of log files for errors Get-ChildItem d:\logs\*.log -recurse | Select-String -pattern '<regex>' Commented Feb 22, 2019 at 10:52
  • 5
    the problem was the wrong place for your Groups property. [grin] use this instead $m.Matches[0].Groups['mylabel']. Commented Feb 22, 2019 at 11:00
  • 2
    @Lee_Dailey Thanks! Big jaw dropper for me is that it is necessary to index into Matches $m.Matches[0] here while when using the numerical index of the regex group things works without that. Commented Feb 22, 2019 at 11:09
  • 1
    you are welcome! the problem was the way that PoSh will give you an array of the properties from a collection when you use $Collection.PropName the result can be a tad strange. [grin] Commented Feb 22, 2019 at 11:11

3 Answers 3

3

You got one correct answer in the comment above, but here is how to do it without using the 0 match index:

$m.Matches.groups | ? { $_.Name -eq 'mylabel' } | Select-Object -ExpandProperty Value
Sign up to request clarification or add additional context in comments.

Comments

0

Here's a one-liner:

Select-String -Pattern '(?<mylabel>error \d*)' -InputObject 'Some text Error 5 some text' |
  Select-Object -ExpandProperty Matches |
  ForEach-Object {$_.Groups['mylabel'].Value}

The output:

Error 5

Comments

0

This works for me. I only get one match.

'Some text Error 5 some text' | select-string '(?<mylabel>error \d*)' | 
  % matches | % value

Error 5

Using the -match operator seems to give you the hashtable you want. I'm not sure how to do that with select-string.

'Some text Error 5 some text' -match '(?<mylabel>error \d*)'
$matches.mylabel

Error 5

Here it is with select-string, somewhat awkwardly. You must reference matches[0], then groups is a GroupCollection object with string indexes. Usually there's multiple labels to use in the place of numbers. You wouldn't need the .value in a string context.

$m='Some text Error 5 some text' | Select-String '(?<mylabel>error \d*)'
$m.matches[0].groups['mylabel'].value

Error 5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.