匹配数组时Powershell使用通配符 [英] Powershell Use Wildcards when matching arrays

查看:62
本文介绍了匹配数组时Powershell使用通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been banging my head against a wall for hours now and am looking for some help. To simplify my issue, I have two arrays, one which contains wildcards, and the other which makes use of those wildcards:

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

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

I cannot get PowerShell to recognize that these match.

我的最终目标是得到一个输出,告诉我Purple.102和orange.abc不在$ WildCardArray中。

似乎超级简单!我尝试过的一些事情:

Seems super simple! Some of the things I've tried:

$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外,我将返回所有结果。我尝试过包含,匹配,等于,类似以及它们的所有对立,比较对象,但没有任何效果。我没有错误,只是没有得到预期的结果

I get BLUE!.txt as a result because it's my control with no wildcards. If I change that to -notcontains, I get all of the results returned except BLUE. I've tried contains, match, equals, like and all of their opposites, compare-object, and nothing works. I get no errors, I just do not get the expected results

我尝试将[*]替换为[a-zA-Z]和其他组合,但它将其替换从字面上看,而不是通配符。我不确定自己在做什么错... PSVersion 5.1 Win 10

I tried replacing "*" with [a-zA-Z] and other combinations, but it replaces it literally, and not as a wildcard. I'm not sure what I'm doing wrong.... PSVersion 5.1 Win 10

有人知道为什么类似/匹配/包含的逻辑不起作用吗? ,而我能做些什么呢?不一定要漂亮,它只需要工作

Does anybody know the logic behind WHY like/match/contains doesn't work, and what I can do make it work? It doesn't have to be pretty, it just needs to work

推荐答案


几小时的墙[..]似乎超级简单!

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

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

That's probably a hint that it's not super simple. You're trying to cross match two lists: red to red, yellow, blue.... then Blue to red, yellow, blue... then Green to red, yellow, blue.... 30 comparisons but you only have 5 loops happening.

您需要更多。

$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上的内部循环可以成为过滤器,但是您必须过滤该数组,而不是单个项你的代码es。

and the inner loop over WildCardArray can become a filter, but you have to be filtering the array, not the individual item as your code does.

$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 
   }
}

我想如果需要的话,您可以将其混入一个不清楚的双向过滤器中。

And I guess you could mash that down into an unclear double-where filter if you had to.

$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 $_}) }

这篇关于匹配数组时Powershell使用通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆