Search code examples
powershellpattern-matchingstring-matchinginvoke-webrequest

powershell -match 2 different parts of one URL and get them


I want to get to different parts of a URL that I retrieve with PowerShell (https://repo.anaconda.com/archive/Anaconda3-2022.05-Windows-x86_64.exe) the -match returns true, the first match is a date that I can get correct and the second part is the name of the file (Anaconda3-2022.05-Windows-x86_64.exe) that I can not get it, can someone help me with it, thanks in advance here is my script

    $Uri = 'https://www.anaconda.com/products/distribution#Downloads'
   ( $web = Invoke-WebRequest -Uri $uri)
   ( $downloadurl=$web.Links |Where-Object href -Like "*_64.exe" |Select-Object -First 1 | Select-Object -expand href )
   ( $downloadurl -match "(\d\d\d\d.\d{1,2}).*(Anaconda*.exe)" )
   ($latestversion = "$($Matches[1])")
   ($FileName = "$($Matches[2])")

Solution

  • There's an easier way to parse the filename than building a regex.

    First, split on slash / to get an array of strings. Since the filename is the last one, use [-1] indexer to get it. Like so,

    $downloadurl.split('/')[-1]
    Anaconda3-2022.05-Windows-x86_64.exe
    

    Now that you have a string with filenamme only, split on dash -:

    $downloadurl.split('/')[-1].split('-')
    Anaconda3
    2022.05
    Windows
    x86_64.exe
    

    Finding the version number is then simple indexing to 2nd element of the resulting array, and the filename is already availale from the previous split operation.