我对results = d = [[{'x':1, 'y':2},{'x':1, 'y':2}], , [{'x':3, 'y':4}, {'x':3, 'y':4}]]
循环有疑问。我正在尝试打印文件中不存在的字符串。该代码段在foreach
循环中引发错误,指出在foreach
关键字后缺少'('
开头。如何克服此错误。
要搜索的字符串是文件abc.txt中的“自然”,“风筝”,“地点”,“街道”,“冒险”。
我有以下代码段
foreach
答案 0 :(得分:1)
$Test = (Get-Content -Path .\file.txt | Select-String -Pattern $Pattern -AllMatches) | foreach {$_.matches.Value}
foreach ($t in $Test) {
$Pattern -split('\|')|where{$Test -notcontains $_}
}
答案 1 :(得分:0)
我会采用其他方式:
|
连接起来来构建正则表达式Sort-Object -Unique
个结果中的Select-String
## Q:\Test\2019\06\07\SO_56488287.ps1
$strings = @('nature','kite','venue','street','venture')
$Pattern = [RegEx]($strings -join '|')
$file = '.\file.txt'
$Found = (Get-Content -Path $file |
Select-String -Pattern $Pattern -AllMatches
).Matches.Value | Sort-Object -Unique
$Missing = $Strings | Where {$Found -notcontains $_}
if($Missing){
"Strings missing in $file"
$Missing
} else {
"All strings present in $file"
}