Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String PowerShell-如何检查字符串以查看它是否包含另一个带有通配符的字符串?_String_Powershell_For Loop_Pattern Matching_Wildcard - Fatal编程技术网

String PowerShell-如何检查字符串以查看它是否包含另一个带有通配符的字符串?

String PowerShell-如何检查字符串以查看它是否包含另一个带有通配符的字符串?,string,powershell,for-loop,pattern-matching,wildcard,String,Powershell,For Loop,Pattern Matching,Wildcard,我想浏览一个文件列表,检查每个文件名是否与列表中的任何字符串匹配。这是我到目前为止所拥有的,但它没有找到任何匹配项。我做错了什么 $files = $("MyApp.Tests.dll","MyApp.Tests.pdb","MyApp.dll") $excludeTypes = $("*.Tests.dll","*.Tests.pdb") foreach ($file in $files) { $containsString = foreach ($type in $Exclude

我想浏览一个文件列表,检查每个文件名是否与列表中的任何字符串匹配。这是我到目前为止所拥有的,但它没有找到任何匹配项。我做错了什么

$files = $("MyApp.Tests.dll","MyApp.Tests.pdb","MyApp.dll")
$excludeTypes = $("*.Tests.dll","*.Tests.pdb")

foreach ($file in $files) 
{
    $containsString = foreach ($type in $ExcludeTypes) { $file | %($_ -match '$type') }

    if($containsString -contains $true)
    {
        Write-Host "$file contains string."
    }
    else
    {
        Write-Host "$file does NOT contains string."
    }
}

对于通配符,您希望使用
-like
运算符而不是
-match
,因为后者需要正则表达式。例如:

$files = @("MyApp.Tests.dll","MyApp.Tests.pdb","MyApp.dll")
$excludeTypes = @("*.Tests.dll","*.Tests.pdb")

foreach ($file in $files) {
    foreach ($type in $excludeTypes) {
        if ($file -like $type) { 
            Write-Host ("Match found: {0} matches {1}" -f $file, $type)
        }
    }
}