Browse Source

Get-NameRegex: finish parameter Exclude feature, modify process code to scriptblock for recurse

feature/NameRegex
pcnick 3 years ago
parent
commit
404b4d41a3
  1. 44
      NameRegex.ps1

44
NameRegex.ps1

@ -2,30 +2,49 @@
param( param(
$Str, $Str,
$Regex, $Regex,
$Exclude,
[switch] $End [switch] $End
) )
# Regex 可輸入複數個 # 查詢 Regex 字串中定義的 TAG,並將其作為 Scriptblock
Write-Host $Str $Keyfind = {
$Regex | % { process{
$key = if ($_ -match "\(\?\<(.*?)\>") { if ($_ -match "\(\?\<(.*?)\>") {
$Matches[1] $Matches[1]
} else { } else {
Write-Error "$_ 沒有定義(?<TAG>)" Write-Error "$_ 沒有定義(?<TAG>)"
} }
if ($Str -match $_ -and($key)) { }
}
# 將原函式轉為 Scriptblock,以方便 Exclude 功能遞迴呼叫
$InnerScrip = {
# 取用管線傳入的 hash 進行 match
process {
$Key = $_["Regex"] | &$Keyfind
if ($_["Target"] -match $_["Regex"]) {
$Result = @{ $Result = @{
$key = $Matches[$key] $Key = $Matches[$Key]
# 將錨定字串中的特殊字元轉換為一般字串 # 將錨定字串中的特殊字元轉換為一般字串
Anchor = $Matches[0] -replace "([\[\]\(\)])", '\$1' Anchor = $Matches[0] -replace "([\[\]\(\)])", '\$1'
} }
if (-not($End)) { $LR = $_["Target"] -split $Result["Anchor"]
$LR = $Str -split $Result["Anchor"]
$Result.Add("L", $LR[0]) $Result.Add("L", $LR[0])
$Result.Add("R", $LR[1]) $Result.Add("R", $LR[1])
} else { $Result.Remove("Anchor") } # 對 Anchor 結果,以 $Exclude 進行比對,符合則加入剩餘字串重新 match
$Result 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 = @( $Pathes = @(
@ -40,7 +59,7 @@ $Pathes = @(
) )
$TestName = @($Pathes | % {$_.Split("\\")[-1]}) $TestName = @($Pathes | % {$_.Split("\\")[-1]})
$Str = $TestName[0] $Str = $TestName[3]
$Regex = @( $Regex = @(
"S\d{2}[\s\-]+E?(?<Episode>\d{2})" "S\d{2}[\s\-]+E?(?<Episode>\d{2})"
"[\[\b\s](?<Episode>\d{2})[\s_]?(?:[vV]\d|END|FIN)[\]\b\s]" "[\[\b\s](?<Episode>\d{2})[\s_]?(?:[vV]\d|END|FIN)[\]\b\s]"
@ -55,4 +74,5 @@ $NameRegex = @(
"[\[\b\s](?<Name>[^\[\]]*)[\]\b\s]?$" "[\[\b\s](?<Name>[^\[\]]*)[\]\b\s]?$"
) )
$RS1 | % { Get-NameRegex $_["L"] -Regex $NameRegex -End } $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) + $_ }
Loading…
Cancel
Save