我有一个文本文件domains.txt
$domains = ‘c:\domains.txt’
$list = Get-Content $domains
google.com
google.js
和一个数组
$array = @(".php",".zip",".html",".htm",".js",".png",".ico",".0",".jpg")
$ domain中以@arr结尾的任何内容都不应该在我的最终列表中
所以google.com会在最终列表中,但google.js不会。
我发现了一些其他的stackoverflow代码,它给了我与我正在寻找的完全相反的东西,但是,我无法让它反转!!!!
这让我与我想要的完全相反,我该如何扭转呢?
$domains = ‘c:\domains.txt’
$list = Get-Content $domains
$array = @(".php",".zip",".html",".htm",".js",".png",".ico",".0",".jpg")
$found = @{}
$list | % {
$line = $_
foreach ($item in $array) {
if ($line -match $item) { $found[$line] = $true }
}
}
$found.Keys | write-host
这给了我google.js我需要它给我google.com。
我尝试过-notmatch等,但无法让它反转。
先谢谢,越多解释就越好!
答案 0 :(得分:0)
关闭.
,将项目混合成正则表达式OR
,在字符串结尾锚点上标记,然后针对它过滤域名。
$array = @("php","zip","html","htm","js","png","ico","0","jpg")
# build a regex of
# .(php|zip|html|htm|...)$
# and filter the list with it
$list -notmatch "\.($($array -join '|'))`$"
无论如何,反转结果的简单方法是遍历$found.keys | where { $_ -notin $list }
。或者将测试更改为$line -notmatch $item
。
但请注意,您正在进行正则表达式匹配,top500.org
之类的内容会与.0
匹配,并将结果抛出。如果您需要在最后进行匹配,则需要使用$line.EndsWith($item)
。
答案 1 :(得分:0)
其他解决方案
$array = @(".php",".zip",".html",".htm",".js",".png",".ico",".0",".jpg")
get-content C:\domains.txt | where {[System.IO.Path]::GetExtension($_) -notin $array}