我有一個由以下形式的 URLS 組成的陣列:
$URLs = @("https://somesite.com/folder1/page/1/"
,"https://somesite.com/folder222/page/1/"
,"https://somesite.com/folder222/page/2/"
,"https://somesite.com/folder444/page/1/"
,"https://somesite.com/folder444/page/3/"
,"https://somesite.com/folderBBB/page/1/"
,"https://somesite.com/folderBBB/page/5/")
他們總是有/page/1/,我需要添加(或重建)從最高頁面到1的所有丟失的URL,所以它最終是這樣的:
$URLs = @("https://somesite.com/folder1/page/1/"
,"https://somesite.com/folder222/page/1/"
,"https://somesite.com/folder222/page/2/"
,"https://somesite.com/folder444/page/1/"
,"https://somesite.com/folder444/page/2/"
,"https://somesite.com/folder444/page/3/"
,"https://somesite.com/folderBBB/page/1/"
,"https://somesite.com/folderBBB/page/2/"
,"https://somesite.com/folderBBB/page/3/"
,"https://somesite.com/folderBBB/page/4/"
,"https://somesite.com/folderBBB/page/5/")
我想偽代碼會是這樣的:
- 對于每個檔案夾,提取最高頁碼:
hxxps://somesite.com/folderBBB/page/5/
將其從 (5) 擴展到 (1)
hxxps://somesite.com/folderBBB/page/1/ hxxps://somesite.com/folderBBB/page/2/ hxxps://somesite.com/folderBBB/page/3/ hxxps://somesite.com/folderBBB/page/4/ hxxps://somesite.com/folderBBB/page/5/將其輸出到陣列中
歡迎任何指點!
uj5u.com熱心網友回復:
您可以通過cmdlet 使用基于管道的解決方案Group-Object,如下所示:
$URLs = @("https://somesite.com/folder1/page/1/"
, "https://somesite.com/folder222/page/1/"
, "https://somesite.com/folder222/page/2/"
, "https://somesite.com/folder444/page/1/"
, "https://somesite.com/folder444/page/3/"
, "https://somesite.com/folderBBB/page/1/"
, "https://somesite.com/folderBBB/page/5/")
$URLs |
Group-Object { $_ -replace '[^/] /$' } | # Group by shared prefix
ForEach-Object {
# Extract the start and end number for the group at hand.
[int] $from, [int] $to =
($_.Group[0], $_.Group[-1]) -replace '^. /([^/] )/$', '$1'
# Generate the output URLs.
# You can assign the entire pipeline to a variable
# ($generatedUrls = $URLs | ...) to capture them in an array.
foreach ($i in $from..$to) { $_.Name $i '/' }
}
筆記:
假設是每組 URL 中共享相同前綴的第一個和最后一個元素始終分別包含所需列舉的起點和終點。
如果該假設不成立,請改用以下內容:
$minMax = $_.Group -replace '^. /([^/] )/$', '$1' | Measure-Object -Minimum -Maximum $from, $to = $minMax.Minimum, $minMax.Maximum
基于正則運算式的
-replace運算子用于兩件事:-replace '[^/] /$'從每個 URL 中洗掉最后一個組件,以便按它們的共享前綴對它們進行分組。-replace '^. /([^/] )/$', '$1'有效地從每個給定的 URL 中提取最后一個組件,即表示所需列舉的起點和終點的數字。
程式替代:
# Build a map (ordered hashtable) that maps URL prefixes
# to the number suffixes that occur among the URLs sharing
# the same prefix.
$map = [ordered] @{}
foreach ($url in $URLs) {
if ($url -match '^(. )/([^/] )/') {
$prefix, [int] $num = $Matches[1], $Matches[2]
$map[$prefix] = [array] $map[$prefix] $num
}
}
# Process the map to generate the URLs.
# Again, use something like
# $generatedUrls = foreach ...
# to capture them in an array.
foreach ($prefix in $map.Keys) {
$nums = $map[$prefix]
$from, $to = $nums[0], $nums[-1]
foreach ($num in $from..$to) {
'{0}/{1}/' -f $prefix, $num # synthesize URL and output it.
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/462222.html
