Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 316
Marc Kean Avatar asked Oct 14 '25 23:10

Marc Kean


1 Answers

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}
like image 63
beatcracker Avatar answered Oct 17 '25 12:10

beatcracker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!