简体   繁体   中英

PowerShell RegEx match all possible matches

I have the following script which includes some RegEx to capture specific information on this site.

$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. You have to use Select-String with -AllMatches parameter 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}

[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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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