检查数组中的字符串

时间:2019-03-21 18:28:13

标签: powershell

我正在寻找如何检查数组值中的字符串值。

我的数组[array]$a01包含如下值

WQA-JK-KUC-I01-hjkl-192.178.10.128/27
WQA-JK-KUC-I01-hjkl-192.178.10.160/27
WQA-JK-KUC-P02-hjkl-192.178.9.64/27
WQA-JK-KUC-P02-hjkl-192.178.9.96/27
WQA-JK-KUC-P01-NCI-hjkl-192.178.9.224/27
WQA-JK-KUC-P01-NCI-hjkl-192.178.10.0/27
WQA-JK-KUC-I01-EAD-194.270.24.0/24
WQA-JK-KUC-I01-KAT-194.270.28.0/24
WQA-JK-KUC-I01-ETKK-194.270.32.0/24
WQA-JK-KUC-I01-hjkl-192.178.10.64/27
WQA-JK-KUC-I01-hjkl-192.178.10.96/27
WQA-JK-KUC-I01-hjkl-192.178.10.192/27
WQA-JK-KUC-P01-NCI-hjkl-192.178.9.160/27
WQA-JK-KUC-P01-NCI-hjkl-192.178.10.32/27
WQA-JK-KUC-P01-NCI-hjkl-192.178.9.192/27
WQA-JK-KUC-P01-NCI-EAD-199.198.168.0/23
WQA-JK-KUC-P01-NCI-KAT-199.198.176.0/23
WQA-JK-KUC-P01-NCI-ETKK-199.198.184.0/23
WQA-JK-KUC-P01-NCI-EAD-199.198.170.0/23
WQA-JK-KUC-P01-NCI-KAT-199.198.178.0/23
WQA-JK-KUC-P01-NCI-ETKK-199.198.186.0/23
WQA-JK-KUC-P01-KAT-194.270.168.0_23

我有两个变量

$v01 = 'abc-ekc-20-ghjkl-79-cis-4321-WQA-JK-KUC-P01-KAT-194.270.168.0_23'
$v02 = 'abc-ekc-20-ghjkl-79-cis-4321-WQA-JK-KUC-P01-NCI-hjkl-192.178.9.160/27'

我写代码

if (($v01 -in $a01) -and ($v02 -in $a01)) {
    Write-Host "It is part of the array"
} else {
    "Not in array"
}

输出为:“不在数组中”。

1 个答案:

答案 0 :(得分:0)

要仅检查以WQA开头的部分,请先用zerolength look ahead assertion拆分内容

-and运算符需要前划线。

if ((($v01 -split '(?=WQA)')[-1] -in $a01) -and 
    (($v02 -split '(?=WQA)')[-1] -in $a01)) {
    Write-Host "It is part of the array"
} else {
    "Not in array"
}