You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.9 KiB
52 lines
1.9 KiB
function Get-NameRegex { |
|
param( |
|
$Str, |
|
$Regex, |
|
$Exclude, |
|
[switch] $End |
|
) |
|
# 查詢 Regex 字串中定義的 TAG,並將其作為 Scriptblock |
|
$Keyfind = { |
|
process{ |
|
if ($_ -match "\(\?\<(.*?)\>") { |
|
$Matches[1] |
|
} else { |
|
Write-Error "$_ 沒有定義(?<TAG>)" |
|
} |
|
} |
|
} |
|
|
|
# 將原函式轉為 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) + $_ } |