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

标签 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

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

最佳答案

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/

相关文章:

php - 如何替换字符串中除第一个字符之外的字符的出现

regex - 在 Powershell 中使用函数替换

string - 如何延迟 PowerShell 字符串中变量的扩展?

powershell - 如何在 PowerShell 中将 Ticks 字符串转换为日期时间?

datetime - PowerShell查询的SCCM LastLogonTimestamp格式

powershell - 我可以使用变量访问对象的嵌套属性吗?

出于性能原因,Java String.split 传递预编译的正则表达式

java - 在多个分隔符上标记字符串

php - cURL 和重定向 - 返回多个 header ?

PowerShell:有没有办法获取通过管道传输到函数中的对象总数?