Powershell 匹配数组时使用通配符

Powershell Use Wildcards when matching arrays

我已经用头撞墙好几个小时了,正在寻求帮助。为了简化我的问题,我有两个数组,一个包含通配符,另一个使用这些通配符:

$WildCardArray = @("RED-*.htm", "*.yellow", "BLUE!.txt", "*.green", "*.purple")
$SpelledOutArray = @("RED-123.htm", "456.yellow", "BLUE!.txt",  "789.green", "purple.102", "orange.abc")

我无法让 PowerShell 识别这些匹配项。

我的最终目标是让输出告诉我 purple.102 和 orange.abc 不在 $WildCardArray 中。

看起来超级简单!我尝试过的一些事情:

$WildCardArray = @("RED-*.htm", "*.yellow", "BLUE!.txt", "*.green", "*.purple")
$SpelledOutArray = @("RED-123.htm", "456.yellow", "BLUE!.txt",  "789.green", "purple.102", "orange.abc")
foreach($Item in $SpelledOutArray)
{
$item | where {$wildcardarray -contains $item}
}

结果我得到了 BLUE!.txt,因为它是我的控件,没有通配符。如果我将其更改为 -notcontains,我将返回除 BLUE 之外的所有结果。我试过包含、匹配、等于、喜欢和它们的所有对立面、比较对象,但没有任何效果。我没有收到任何错误,只是没有得到预期的结果

我尝试用 [a-zA-Z] 和其他组合替换“*”,但它按字面意思替换了它,而不是作为通配符。我不确定自己做错了什么.... PSVersion 5.1 Win 10

有人知道为什么 like/match/contains 不起作用背后的逻辑吗?我可以做些什么让它起作用?它不必很漂亮,它只需要工作

$WildCardArray = @("RED-*.htm", "*.yellow", "BLUE!.txt", "*.green", "*.purple")
$SpelledOutArray = @("RED-123.htm", "456.yellow", "BLUE!.txt",  "789.green", "purple.102", "orange.abc")

$WildCardArray | %{$str=$_; $SpelledOutArray | ? {$_ -like $str}  }

其他解决方案,不短

$WildCardArray | 
   %{$current=$_; $SpelledOutArray | %{ [pscustomobject]@{wildcard=$current; value=$_ }}} | 
        where {$_.value -like $_.wildcard } 

banging my head against a wall for hours [..] Seems super simple!

这可能暗示它不是超级简单。您正在尝试交叉匹配两个列表:红色到红色、黄色、蓝色....然后蓝色到红色、黄色、蓝色...然后绿色到红色、黄色、蓝色...。 30 次比较,但你只发生了 5 次循环。

你还需要更多。

$WildCardArray = @("RED-*.htm", "*.yellow", "BLUE!.txt", "*.green", "*.purple")
$SpelledOutArray = @("RED-123.htm", "456.yellow", "BLUE!.txt",  "789.green", "purple.102", "orange.abc")

# Loop once over the spelled out items
foreach($Item in $SpelledOutArray)
{
    # for each one, loop over the entire WildCard array and check for matches
    $WildCardMatches = foreach ($WildCard in $WildCardArray)
    { 
        if ($item -like $WildCard) {
            $Item
        }
    }

    # Now see if there were any wildcard matches for this SpelledOut Item or not
    if (-not $WildCardMatches)
    {
        $Item 
    }
}

并且 WildCardArray 上的内部循环可以成为过滤器,但您必须过滤数组,而不是像您的代码那样过滤单个项目。

$WildCardArray = @("RED-*.htm", "*.yellow", "BLUE!.txt", "*.green", "*.purple")
$SpelledOutArray = @("RED-123.htm", "456.yellow", "BLUE!.txt",  "789.green", "purple.102", "orange.abc")

foreach($Item in $SpelledOutArray)
{
   $WildCardMatches = $wildcardarray | Where { $item -like $_ }

   if (-not $WildCardMatches)
   {
       $Item 
   }
}

而且我想如果需要的话,您可以将其分解成一个不清楚的双位置过滤器。

$WildCardArray = @("RED-*.htm", "*.yellow", "BLUE!.txt", "*.green", "*.purple")
$SpelledOutArray = @("RED-123.htm", "456.yellow", "BLUE!.txt",  "789.green", "purple.102", "orange.abc")

$SpelledOutArray |Where {$item=$_; -not ($WildCardArray |Where {$item -like $_}) }

您的通配符数组实际上是要查找的模式列表。您可以将其转换为单个正则表达式并与之匹配:

$WildCardArray = @("RED-*.htm", "*.yellow", "BLUE!.txt", "*.green", "*.purple")
$SpelledOutArray = @("RED-123.htm", "456.yellow", "BLUE!.txt",  "789.green", "purple.102", "orange.abc")

# Turn wildcards into regexes
# First escape all characters that might cause trouble in regexes (leaving out those we care about)
$escaped = $WildcardArray -replace '[ #$()+.[\^{]','$&' # list taken from Regex.Escape
# replace wildcards with their regex equivalents
$regexes = $escaped -replace '\*','.*' -replace '\?','.'
# combine them into one regex
$singleRegex = ($regexes | %{ '^' + $_ + '$' }) -join '|'

# match against that regex
$SpelledOutArray -notmatch $singleRegex

这有可能比在循环中检查所有内容更快,尽管我没有测试。此外,非常长的正则表达式也可能会造成问题。