How to get a regex "group" from a select-string cmdlet

I am scraping a web request response to pull information held within the html code repeats a few times so using select string rather than match. My code looks like

$regexname = '\(\w{0,11}).{1,10}\' $energenie.RawContent | select-string $regexname -AllMatches | % {$_.matches}

The return looks something like:

Groups : {(h2 class="ener")TV (/h2), TV} Success : True Captures : {(h2 class="ener")TV (/h2)} Index : 1822 Length : 33 Value : (h2 class="ener")TV (/h2)

Groups : {(h2 class=“ener”)PS3 (/h2), PS3}
Success : True
Captures : {(h2 class=“ener”)PS3 (/h2)}
Index : 1864
Length : 33
Value : (h2 class=“ener”)PS3 (/h2)

N.B. I’ve changed the triangle brackets to circular brackets because of formatting on the forum

I can’t workout a way to grab the second element of groups e.g. TV or PS3 as:

$energenie.RawContent | select-string $regexname -AllMatches | % {$_.matches.groups}

Gives a strange output

Paul

You will need to find out what position your group is in and then specify it like you would an array

$energenie.RawContent | select-string $regexname -AllMatches | % {$_.matches.groups[0].value}
$energenie.RawContent | select-string $regexname -AllMatches | % {$_.matches.groups[1].value}
$energenie.RawContent | select-string $regexname -AllMatches | % {$_.matches.groups[2].value}

Curtis,

Thanks for the feedback. I crossed post on to Stackoverflow and a response made me realise I was make a incorrect assumption. I had the impression that a piped ForEach-Object would iterate through the elements and I could grab the second item in the groups array, but it does appear it works like this, instead I created a variable to hold the match output and then stepped through every other group element using a For loop:

$socketname = ($energenie.RawContent | select-string $regexname -AllMatches).matches
For ($i = 1; $i -lt ($socketname.groups).count; $i+=2) {
Write-Output “$(($socketname.groups[$i].Value).Trim())”
}

@paul-bendall, that seems a little overkill for the result you are getting. If you are just wanting to get the value of all of the match groups you can do this.

$energenie.RawContent | Select-String -Pattern $regexname -AllMatches | ForEach-Object {$_.matches.groups.value}