PowerShell RegEx 匹配所有可能的匹配项

Mar*_*ean 3 regex powershell

我有以下脚本,其中包含一些 RegEx 来捕获此站点上的特定信息。

$Top40Response = Invoke-WebRequest -UseBasicParsing -Uri 'https://www.radioinfo.com.au/knowledge/chart'

$Top40Response.Content -match '<td\Wclass="twRank">[\s\S]+artist">([^<]*)'
$matches
Run Code Online (Sandbox Code Playgroud)

这是匹配最后一个“艺术家”。我想要做的就是制作它,以便它会按照从上到下的顺序贯穿并匹配此页面上的每个艺术家。

bea*_*ker 5

PowerShell-match只返回第一个匹配项。您必须使用Select-Stringwith-AllMatches参数 or [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}
Run Code Online (Sandbox Code Playgroud)

[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}
Run Code Online (Sandbox Code Playgroud)