我有一个文本文件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不会。
我发现了一些其他的堆栈溢出代码,给了我与我正在寻找的完全相反的东西,但是,哈,我无法逆转它!
这给了我与我想要的完全相反的东西,我如何扭转它呢?
$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等,但无法使它逆转。
提前谢谢,解释越多越好!
发布于 2016-11-11 06:52:16
取下.,将项目合并到正则表达式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)的东西。
发布于 2016-11-11 10:07:54
其他解决方案
$array = @(".php",".zip",".html",".htm",".js",".png",".ico",".0",".jpg")
get-content C:\domains.txt | where {[System.IO.Path]::GetExtension($_) -notin $array}https://stackoverflow.com/questions/40542525
复制相似问题