function Get-NameRegex { param( $Str, $Regex, $Exclude, [switch] $End ) # 查詢 Regex 字串中定義的 TAG,並將其作為 Scriptblock $Keyfind = { process{ if ($_ -match "\(\?\<(.*?)\>") { $Matches[1] } else { Write-Error "$_ 沒有定義(?)" } } } # 將原函式轉為 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]) # 對 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 } # Example # $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) + $_ } # SIG # Begin signature block # MIIFxwYJKoZIhvcNAQcCoIIFuDCCBbQCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB # gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR # AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQU2SfubhaWpugweGH+53eZ/igq # VQOgggNSMIIDTjCCAjagAwIBAgIQdMnAQ4BDAo9Lgfn+QeDdhjANBgkqhkiG9w0B #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