[英]PowerShell RegEx match all possible matches
我有以下腳本,其中包括一些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}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.