0

I am scraping a web request response to pull information held within html code which 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>

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

Paul B
  • 351
  • 1
  • 3
  • 5

2 Answers2

1

This should work:

$energenie.RawContent | select-string $regexname -AllMatches | ForEach-Object { Write-Host $_.Matches.Groups[1].Value }

Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41
Buxmaniak
  • 460
  • 2
  • 4
  • Thanks @rohitgupta I now realised I was making an assumption and asked the wrong question. I was wanting a piped ForEach-Object to either return a list of unique names, TV, PS3 or repeat the same value representing a count of matches TV, TV due to the Index. I've now used your example to capture the groups collection but step through every other element to get TV, PS3 – Paul B Oct 26 '15 at 22:31
  • @PaulB, I only edited it. Buxmaniak actually posted this. If this is not rubbish then you should upvote it. And if it solves your problem then you should tick it. – Rohit Gupta Oct 27 '15 at 00:19
0

To get the second item in a collection, use the array index operator: [n] where n is the index from which you want a value.

For each entry in Matches, you want the second entry in the Groups property so that would be:

$MyMatches = $energenie.RawContent | select-string $regexname -AllMatches | % {$_.Matches}
$SecondGroups = $MyMatches | % {$_.Groups[1]}

To get just the captured value, use the Value property:

$MyMatches | % { $_.Groups[1].Value }
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Thanks @MathiasRJessen as described above I realised from the answers is that what I wanted was every other element in the array – Paul B Oct 26 '15 at 22:34
  • Every second match in $_.Matches or every second Group in each match? – Mathias R. Jessen Oct 26 '15 at 23:01
  • Every second in groups. The code ended up as: `$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 B Oct 27 '15 at 20:51