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 # VQOgggNSMIIDTjCCAjagAwIBAgIQV5lzhhF94bJEhqpMRjsGlzANBgkqhkiG9w0B # AQU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