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

2 Answers 2

1

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 }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @MathiasRJessen as described above I realised from the answers is that what I wanted was every other element in the array
Every second match in $_.Matches or every second Group in each match?
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())" }
1

This should work:

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

2 Comments

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
@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.

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.