diff --git a/t2.ps1 b/t2.ps1 new file mode 100644 index 0000000..1b4bf79 --- /dev/null +++ b/t2.ps1 @@ -0,0 +1,162 @@ +function Get-NameRegex { + param( + $Str, + $Regex, + $Exclude, + [switch] $End + ) + # Regex 可輸入複數個 + Write-Host $Str + $Regex | % { + $key = if ($_ -match "\(\?\<(.*?)\>") { + $Matches[1] + } else { + Write-Error "$_ 沒有定義(?)" + } + if ($Str -match $_ -and($key)) { + $Result = @{ + $key = $Matches[$key] + # 將錨定字串中的特殊字元轉換為一般字串 + Anchor = $Matches[0] -replace "([\[\]\(\)])", '\$1' + } + if (-not($End)) { + $LR = $Str -split $Result["Anchor"] + $Result.Add("L", $LR[0]) + $Result.Add("R", $LR[1]) + } else { $Result.Remove("Anchor") } + $Result + } + if ($Exclude) { + # 以 hash 送入 scriptblock + $InterSB = { + # 取用管線傳入的 hash 進行 match + if ($_["Target"] -match $_["Regex"]) { + $Result = @{ + $key = $Matches[$key] + # 將錨定字串中的特殊字元轉換為一般字串 + Anchor = $Matches[0] -replace "([\[\]\(\)])", '\$1' + } + $LR = $_["Target"] -split $Result["Anchor"] + $Result.Add("L", $LR[0]) + $Result.Add("R", $LR[1]) + # 對 Matches 結果,比對 $Exclude + if ($Result["Anchor"] -match $Exclude) { + $Result.Add("Regex", $_["Regex"]) + $Result.Add("Target", $Result["L"]) + } + } + } + } + } +} + +$Pathes = @( + "L:\Anime\201110 WORKING’!!\[#CHAT RUMBLE#][WORKING’!!][01-13 END][BIG5][x264_aac][1280x720]\[#CHAT RUMBLE#][WORKING’!!][13][BIG5][x264_aac][1280x720][11C6E807].mp4" + "L:\Anime\200910 にゃんこい!(貓願三角戀)\[DMG][Nyan Koi!][01-12 END][848x480][BIG5]\[DMG][Nyan Koi!][12 end][848x480][BIG5].rmvb" + "L:\Anime\200910 聖剣の刀鍛冶(聖劍鍛造師)\[DMG][The Sacred Blacksmith][01-12 END][848x480][BIG5]\[DMG][The Sacred Blacksmith][04V2][848x480][BIG5].rmvb" + "L:\Anime\201104 星空へ架かる橋(架向星空之橋)\[AngelSub][Hoshizora e Kakaru Hashi][BDRip][Vol.01][1920x1080][TW_BIG5][x264_FLAC]\[AngelSub][Hoshizora e Kakaru Hashi][BDRip][01][1920x1080][BIG5][x264_FLAC].mkv" + "H:\Anime\201901 盾の勇者の成り上がり\[LKSUB][Tate no Yuusha no Nariagari][01-25][GB][720P][MP4]\[LKSUB][Tate no Yuusha no Nariagari][01][GB][720P].mp4" + "L:\Anime\202004 ストライク・ザ・ブラッド OVA IV\[Suzu-Kaze][Strike_the_Blood_IV_OVA][01-12FIN][BDRip_1080P][HEVC_YUV420P10]\[Suzu-Kaze] Strike the Blood IV 12FIN [BDRip 1920x1080 HEVC YUV420P10 FLAC].mkv" + "L:\Anime\200910 アスラクライン II(機巧魔神)\[DMG][Asura Cryin2][01-13 END][848x480][BIG5][RMVB]\[DMG][Asura Cryin 2][01][848x480][Big5].rmvb" + "H:\Data\Downloads\異世界食堂\[Lilith-Raws] Isekai Shokudou S02 - 06 [Baha][WEB-DL][1080p][AVC AAC][CHT][MP4].mp4" +) +$TestName = @($Pathes | % {$_.Split("\\")[-1]}) + +$Str = $TestName[3] +$Regex = @( + "S\d{2}[\s\-]+E?(?\d{2})" + "[\[\b\s](?\d{2})[\s_]?(?:[vV]\d|END|FIN)[\]\b\s]" + "\[(?\d{1,2})\]" + #"\d" +) + +$RS1 = $TestName | % { Get-NameRegex $_ -Regex $Regex } +$RS1 + +$NameRegex = @( + "[\[\b\s](?[^\[\]]*)[\]\b\s]?$" +) + +$RS1 | % { Get-NameRegex $_["L"] -Regex $NameRegex -End } + +#if ($Exclude) { + + $Keyfind = { + process{ + if ($_ -match "\(\?\<(.*?)\>") { + $Matches[1] + } else { + Write-Error "$_ 沒有定義(?)" + } + } + } + # 以 hash 送入 scriptblock + $InterSB = { + # 取用管線傳入的 hash 進行 match + param($Hobj, $Target, $Regex, $Key) + Write-Host $hash, $_ + process { + if ($Hobj) { $hash = $Hobj } else { $hash = $_ } + + + if ($hash) { + $Target = $hash["Target"] + $Regex = $hash["Regex"] + if ($hash.ContainsKey("Key")) { $Key = $hash["Key"] } + } + if (-not($Key)) { $Key = $Regex | &$Keyfind } + if ($Target -match $Regex) { + $Result = @{ + $key = $Matches[$key] + # 將錨定字串中的特殊字元轉換為一般字串 + Anchor = $Matches[0] -replace "([\[\]\(\)])", '\$1' + } + $LR = $hash["Target"] -split $Result["Anchor"] + $Result.Add("L", $LR[0]) + $Result.Add("R", $LR[1]) + # 對 Matches 結果,比對 $Exclude + if ($Result["Anchor"] -match $Exclude) { + $Result.Add("Regex", $hash["Regex"]) + $Result.Add("Target", $Result["L"]) + } + }} + } + + $InterSB2 = { + # 取用管線傳入的 hash 進行 match + process { + if (-not($Key)) { $Key = $_["Regex"] | &$Keyfind } + if ($_["Target"] -match $_["Regex"]) { + $Result = @{ + $Key = $Matches[$Key] + # 將錨定字串中的特殊字元轉換為一般字串 + Anchor = $Matches[0] -replace "([\[\]\(\)])", '\$1' + } + $LR = $_["Target"] -split $Result["Anchor"] + $Result.Add("L", $LR[0]) + $Result.Add("R", $LR[1]) + # 對 Matches 結果,比對 $Exclude + if ($Result["Anchor"] -match $Exclude) { + $Result.Add("Regex", $_["Regex"]) + $Result.Add("Target", $Result["L"]) + return $Result | &$InterSB2 + } + $Result + }} + } +# } + +$D1 = $Str | % { Get-NameRegex $_ -Regex $Regex } +$D2 = @{ + Target = $D1["L"] + Regex = $NameRegex[0] + #Exclude = "BDrip" +} +$D2 + +$D2["Target"] +$wee = $D2["Regex"] | &$Keyfind + +$Exclude = "1" +$D2["Target"] +$D2 | &$InterSB2 \ No newline at end of file