简体   繁体   English

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

[英]PowerShell RegEx match all possible matches

I have the following script which includes some RegEx to capture specific information on this site. 我有以下脚本,其中包括一些RegEx来捕获此站点上的特定信息。

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

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

This is matching the last 'artist'. 这与最后一位“艺术家”相匹配。 What I want to do is make this so it will run through and match every artist on this page in order top to bottom. 我要做的是使它贯穿整个页面,并按从上到下的顺序匹配此页面上的每个艺术家。

PowerShell's -match only returns first match. PowerShell的-match仅返回第一个匹配项。 You have to use Select-String with -AllMatches parameter or [regex]::Matches . 您必须将Select-String-AllMatches参数或[regex]::Matches

Select-String : 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 : [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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM