我正在抓取一个Web请求响应来拉取html代码中保存的信息,这些信息会重复几次,因此使用select-string而不是匹配。我的代码看起来像
$regexname = '\(\w{0,11}).{1,10}\'
$energenie.RawContent | select-string $regexname -AllMatches | % {$_.matches}
回报看起来像:
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>
我无法锻炼以获取群组的第二个元素,例如电视或PS3为:
$energenie.RawContent | select-string $regexname -AllMatches | % {$_.matches.groups}
给出一个奇怪的输出
保
答案 0 :(得分:1)
这应该有效:
$ energenie.RawContent | select-string $ regexname -AllMatches | ForEach-Object {Write-Host $ _.matches.Groups [1] .Value}
答案 1 :(得分:0)
要获取集合中的第二项,请使用数组索引运算符:[n]
其中n
是您想要值的索引。
对于Matches
中的每个条目,您需要Groups
属性中的第二个条目,以便:
$MyMatches = $energenie.RawContent | select-string $regexname -AllMatches | % {$_.Matches}
$SecondGroups = $MyMatches | % {$_.Groups[1]}
要获取捕获的值,请使用Value
属性:
$MyMatches | % { $_.Groups[1].Value }