You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell正则匹配JavaRESTfulEngine压缩包URL失败求助

解决PowerShell正则匹配JavaRESTfulEngine下载URL的问题

问题描述

需要通过PowerShell从网页提取符合格式https://<任意内容>/JavaRESTfulEngine<任意内容>.zip的URL,目标URL为https://cdn.windwardstudios.com/Archive/23.X/23.3.0/JavaRESTfulEngine-23.3.0.32.zip。使用精确正则$regexPattern = 'https://cdn\.windwardstudios\.com/Archive/\d{2}\.X/\d+\.\d+\.\d+/JavaRESTfulEngine-.*?\.zip'可成功匹配,但改用通用正则$regexPattern = 'https://cdn\.windwardstudios\.com/Archive/([^/]+)/JavaRESTfulEngine-.*?\.zip'时,脚本输出"Zip file URLs not found on the page.",无法匹配目标URL。

问题原因

目标URL在Archive/后包含两级路径(23.X/23.3.0/),但通用正则中的([^/]+)/仅能匹配一级路径(匹配23.X/后就停止),无法覆盖后续的23.3.0/部分,导致正则表达式与目标URL结构不匹配,最终匹配失败。

解决方案

调整正则表达式,使其能匹配Archive/之后到JavaRESTfulEngine之前的所有路径段(无论层级数量)。可以采用两种修正方式:

方式1:匹配任意路径内容(简洁通用)

将正则改为匹配Archive/后任意非换行内容,直到JavaRESTfulEngine:

$regexPattern = 'https://cdn\.windwardstudios\.com/Archive/.+/JavaRESTfulEngine-.*?\.zip'
  • .+会匹配除换行外的任意字符(包括/),可以覆盖多级路径。

方式2:匹配到HTML引号结束(更严谨)

考虑到网页中URL通常包裹在引号内,用[^"]+匹配到引号终止,避免匹配无关内容:

$regexPattern = 'https://cdn\.windwardstudios\.com/Archive/[^"]+/JavaRESTfulEngine-[^"]+\.zip'

修正后的完整脚本

# URL of the website to scrape
$websiteUrl = "https://www.windwardstudios.com/version/version-downloads"
# Use Invoke-WebRequest to fetch the web page content
$response = Invoke-WebRequest -Uri $websiteUrl
# Check if the request was successful
if ($response.StatusCode -eq 200) {
    # Parse the HTML content to find the zip file URL using a regular expression
    $htmlContent = $response.Content
    # 使用修正后的正则表达式
    $regexPattern = 'https://cdn\.windwardstudios\.com/Archive/.+/JavaRESTfulEngine-.*?\.zip'
    $zipFileUrls = [regex]::Matches($htmlContent, $regexPattern) | ForEach-Object { $_.Value }
    if ($zipFileUrls.Count -gt 0) {
        Write-Host "Found zip file URLs:"
        $zipFileUrls | ForEach-Object { Write-Host $_ }
    } else {
        Write-Host "Zip file URLs not found on the page."
    }
} else {
    Write-Host "Failed to fetch the web page. Status code: $($response.StatusCode)"
}

执行结果

运行修正后的脚本,将输出目标URL:

https://cdn.windwardstudios.com/Archive/23.X/23.3.0/JavaRESTfulEngine-23.3.0.32.zip

内容的提问来源于stack exchange,提问作者Ashar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 11:27:05