我有以下脚本,其中包含一些 RegEx 来捕获此站点上的特定信息。
$Top40Response = Invoke-WebRequest -UseBasicParsing -Uri 'https://www.radioinfo.com.au/knowledge/chart'
$Top40Response.Content -match '<td\Wclass="twRank">[\s\S]+artist">([^<]*)'
$matches
这是匹配最后一个“艺术家”。我想要做的就是制作它,以便它会按照从上到下的顺序贯穿并匹配此页面上的每个艺术家。
最佳答案
PowerShell 的 -match
只返回第一场比赛。您必须使用 Select-String
与 -AllMatches
参数或 [regex]::Matches
.Select-String
:
$Top40Response = Invoke-WebRequest -UseBasicParsing -Uri 'https://www.radioinfo.com.au/knowledge/chart'
$Top40Response.Content |
Select-String -Pattern '<td\s+class="artist">(.*?)<\/td>' -AllMatches |
ForEach-Object {$_.Matches} |
ForEach-Object {$_.Groups[1].Value}
[regex]::Matches
:$Top40Response = Invoke-WebRequest -UseBasicParsing -Uri 'https://www.radioinfo.com.au/knowledge/chart'
$Top40Response.Content |
ForEach-Object {[regex]::Matches($_, '<td\s+class="artist">(.*?)<\/td>')} |
ForEach-Object {$_.Groups[1].value}
关于regex - PowerShell RegEx 匹配所有可能的匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33966812/