Browse Source

NameRegex temple 2

temp1
pcnick 3 years ago
parent
commit
ab77c7b4d2
  1. 130
      t2.ps1

130
t2.ps1

@ -5,49 +5,46 @@
$Exclude, $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)) {
$Result = @{
$key = $Matches[$key]
# 將錨定字串中的特殊字元轉換為一般字串
Anchor = $Matches[0] -replace "([\[\]\(\)])", '\$1'
} }
if (-not($End)) { }
$LR = $Str -split $Result["Anchor"] }
#
$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("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"])
if ($Exclude) { $Result.Add("Target", $Result["L"])
# 以 hash 送入 scriptblock return $Result | &$InnerScrip
$InterSB = {
# 取用管線傳入的 hash 進行 match
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])
# 對 Matches 結果,比對 $Exclude
if ($Result["Anchor"] -match $Exclude) {
$Result.Add("Regex", $_["Regex"])
$Result.Add("Target", $Result["L"])
}
} }
# 結尾模式,僅輸出符合 TAGKEY 的 hash
if ($End) { return @{ $Key = $Result[$Key] } } else { return $Result }
} }
} }
} }
Write-Host $Str
# Regex 可輸入複數個
$Regex | % { @{ Target = $Str; Regex = $_; } } | &$InnerScrip
} }
$Pathes = @( $Pathes = @(
@ -77,51 +74,22 @@ $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 }
#if ($Exclude) { $TestName | % { Get-NameRegex $_ -Regex $Regex }
$TestName | % { Get-NameRegex $_ -Regex $Regex } | % { $(Get-NameRegex $_["L"] -Regex $NameRegex -Exclude "(BD|DVD|HDTV)RIP" -End) + $_ }
$Keyfind = {
process{
if ($_ -match "\(\?\<(.*?)\>") {
$Matches[1]
} else {
Write-Error "$_ 沒有定義(?<TAG>)"
}
}
}
# 以 hash 送入 scriptblock
$InterSB = {
# 取用管線傳入的 hash 進行 match
param($Hobj, $Target, $Regex, $Key)
Write-Host $hash, $_
process {
if ($Hobj) { $hash = $Hobj } else { $hash = $_ }
$D1 = $Str | % { Get-NameRegex $_ -Regex $Regex }
if ($hash) { $Keyfind = {
$Target = $hash["Target"] process{
$Regex = $hash["Regex"] if ($_ -match "\(\?\<(.*?)\>") {
if ($hash.ContainsKey("Key")) { $Key = $hash["Key"] } $Matches[1]
} } else {
if (-not($Key)) { $Key = $Regex | &$Keyfind } Write-Error "$_ 沒有定義(?<TAG>)"
if ($Target -match $Regex) {
$Result = @{
$key = $Matches[$key]
# 將錨定字串中的特殊字元轉換為一般字串
Anchor = $Matches[0] -replace "([\[\]\(\)])", '\$1'
}
$LR = $hash["Target"] -split $Result["Anchor"]
$Result.Add("L", $LR[0])
$Result.Add("R", $LR[1])
# 對 Matches 結果,比對 $Exclude
if ($Result["Anchor"] -match $Exclude) {
$Result.Add("Regex", $hash["Regex"])
$Result.Add("Target", $Result["L"])
}
}}
} }
}
}
$InterSB2 = { $InterSB2 = {
# 取用管線傳入的 hash 進行 match # 取用管線傳入的 hash 進行 match
process { process {
@ -144,16 +112,18 @@ $RS1 | % { Get-NameRegex $_["L"] -Regex $NameRegex -End }
$Result $Result
}} }}
} }
# }
$D1 = $Str | % { Get-NameRegex $_ -Regex $Regex }
$D2 = @{ $D2 = @{
Target = $D1["L"] Target = $D1["L"]
Regex = $NameRegex[0] Regex = $NameRegex[0]
#Exclude = "BDrip" Exclude = "BDrip"
} }
$D2 $D2
$eeww = @("Exclude", "Key")
$D2.Remove($eeww)
$eeww | % { $D2.Remove($_) }
$D2["Target"] $D2["Target"]
$wee = $D2["Regex"] | &$Keyfind $wee = $D2["Regex"] | &$Keyfind

Loading…
Cancel
Save