diff --git a/NameRegex.ps1 b/NameRegex.ps1 index cb47255..d609753 100644 --- a/NameRegex.ps1 +++ b/NameRegex.ps1 @@ -2,30 +2,49 @@ 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' + # 查詢 Regex 字串中定義的 TAG,並將其作為 Scriptblock + $Keyfind = { + process{ + if ($_ -match "\(\?\<(.*?)\>") { + $Matches[1] + } else { + Write-Error "$_ 沒有定義(?)" } - if (-not($End)) { - $LR = $Str -split $Result["Anchor"] + } + } + + # 將原函式轉為 Scriptblock,以方便 Exclude 功能遞迴呼叫 + $InnerScrip = { + # 取用管線傳入的 hash 進行 match + process { + $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]) - } else { $Result.Remove("Anchor") } - $Result + # 對 Anchor 結果,以 $Exclude 進行比對,符合則加入剩餘字串重新 match + if ($Exclude -and($Result["Anchor"] -match $Exclude)) { + $Result.Add("Regex", $_["Regex"]) + $Result.Add("Target", $Result["L"]) + return $Result | &$InnerScrip + } + # 結尾模式,僅輸出符合 TAGKEY 的 hash + if ($End) { return @{ $Key = $Result[$Key] } } else { return $Result } + } } } + + #Write-Host $Str + # Regex 可輸入複數個 + $Regex | % { @{ Target = $Str; Regex = $_; } } | &$InnerScrip } $Pathes = @( @@ -40,7 +59,7 @@ $Pathes = @( ) $TestName = @($Pathes | % {$_.Split("\\")[-1]}) -$Str = $TestName[0] +$Str = $TestName[3] $Regex = @( "S\d{2}[\s\-]+E?(?\d{2})" "[\[\b\s](?\d{2})[\s_]?(?:[vV]\d|END|FIN)[\]\b\s]" @@ -55,4 +74,5 @@ $NameRegex = @( "[\[\b\s](?[^\[\]]*)[\]\b\s]?$" ) -$RS1 | % { Get-NameRegex $_["L"] -Regex $NameRegex -End } \ No newline at end of file +$RS1 | % { Get-NameRegex $_["L"] -Regex $NameRegex -Exclude "(BD|DVD|HDTV)RIP" -End } +$TestName | % { Get-NameRegex $_ -Regex $Regex } | % { $(Get-NameRegex $_["L"] -Regex $NameRegex -Exclude "(BD|DVD|HDTV)RIP" -End) + $_ } \ No newline at end of file