$ignoreList = @("muzi","puzi")
$data = "
blabla aa 11
blabla bb 22
muzi aa 20
muzi bb aa
aaa aa 41
blabla aa 20
puzi aa 11
puzi bb 32
puzi cc 44"
我需要創建新資料,其中包含所有資料,除了同樣在忽略串列中的資料
#i can iterate the list and run a loop, get $str to be the item in the list and
#and then save each time
$data | where-object {$_ -notlike $str}
我認為有一些比每次迭代串列 abd savubg 更好的選擇
uj5u.com熱心網友回復:
-like一次只能處理一種模式(通配符運算式)。
要在單個操作中匹配多個模式,您有兩個選擇:
- 將基于regex的
-notmatch運算子與交替運算式 (|) 一起使用,這要求您對忽略單詞進行轉義[regex]::Escape(),以便將它們逐字用作正則運算式的一部分(對于您的特定搜索詞并非絕對必要,因此在這個簡單的情況下你可以逃脫'^(?:{0})' -f ($ignoreList -join '|')); 使用正則運算式還允許您斷言必須在每個字串 ( )的開頭找到每個忽略詞^:
$ignoreList = @("muzi","puzi")
# Create an *array* of sample lines.
$data = @'
blabla aa 11
blabla bb 22
muzi aa 20
muzi bb aa
aaa aa 41
blabla aa 20
puzi aa 11
puzi bb 32
puzi cc 44"
'@ -split '\r?\n'
# The programmatically created regex results in:
# '^(?:muzi|puzi)'
# The ?: part isn't strictly necessary, but makes the (...) group
# non-capturing, which prevents unnecessary work.
$data -notmatch ('^(?:{0})' -f ($ignoreList.ForEach({ [regex]::Escape($_) }) -join '|'))
- 使用
Select-String多模式的cmdlet的(雖然你可以使用一個單一的一個具有交替也是如此),這可能是字面的搜索字詞如果添加-SimpleMatch。由于使用了管道,這種方法更簡單,但速度更慢:
# Note the need to use (...).Line to extract the matching strings.
# In PowerShell (Core) 7 you could use -Raw instead.
($data | Select-String -Pattern $ignoreList -SimpleMatch -NotMatch).Line
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/393501.html
標籤:电源外壳
