Question:
I have the following script which includes some RegEx to capture specific information on this site.
1 2 3 4 5 |
$Top40Response = Invoke-WebRequest -UseBasicParsing -Uri 'https://www.radioinfo.com.au/knowledge/chart' $Top40Response.Content -match ' [\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.
Answer:
PowerShell’s -match
only returns first match. You have to use Select-String
with -AllMatches
parameter or [regex]::Matches
.
Select-String
:
1 2 3 4 5 6 7 |
$Top40Response = Invoke-WebRequest -UseBasicParsing -Uri 'https://www.radioinfo.com.au/knowledge/chart' $Top40Response.Content | Select-String -Pattern ' (.*?)<\/td>' -AllMatches | |
ForEach-Object {$_.Matches} | ForEach-Object {$_.Groups[1].Value} |
[regex]::Matches
:
1 2 3 4 5 6 |
$Top40Response = Invoke-WebRequest -UseBasicParsing -Uri 'https://www.radioinfo.com.au/knowledge/chart' $Top40Response.Content | ForEach-Object {[regex]::Matches($_, ' (.*?)<\/td>')} | |
ForEach-Object {$_.Groups[1].value} |