英文:
Compare string of array to another string of array
问题
I'm trying to compare a string within an array to another string within another array
I feel like the logic is right but for some reason couldn't get the expected output
The array $groupslist has values of "cn-sg-robot-platform", "cn-sg-capture-platform", "cn-sg-easy-platform", "cn-sg-content-platform"
The array $comparestrings has values "sg-robot-platform" , "sg-easy-platform"
I want to print a message if $comparestring value is present in $groupslist ( if sg-robot-platform is present in cn-sg-robot-platform it should print a message )
The code which I tried isn't performing pattern search
$AdGroup = "cn-sg-robot-platform","cn-sg-capture-platform","cn-sg-easy-platform","cn-sg-content-platform"
$comparestrings = "sg-robot-platform" , "sg-easy-platform"
foreach ($item in $AdGroup) {
$matchFound = $false
foreach ($dev in $comparestrings) {
if ($dev -like "*$item*") {
$matchFound = $true
echo "Match found"
break
}
}
if (-not $matchFound) {
Write-Host "No match found for $item"
}
}
**I want to print the match found when sg-robot-platform matches with cn-sg-robot-platform pattern **
英文:
I'm trying to compare a string within an array to another string within another array
I feel like the logic is right but for some reason couldn't get the expected output
The array $groupslist has values of "cn-sg-robot-platform","cn-sg-capture-platform","cn-sg-easy-platform","cn-sg-content-platform"
The array $comparestrings has values "sg-robot-platform" , "sg-easy-platform"
I want to print a message if $comparestring value is present in $groupslist ( if sg-robot-platform is present in cn-sg-robot-platform it should print a message )
The code which I tried isn't performing pattern search
$AdGroup = "cn-sg-robot-platform","cn-sg-capture-platform","cn-sg-easy-platform","cn-sg-content-platform"
$comparestrings = "sg-robot-platform" , "sg-easy-platform"
foreach ($item in $AdGroup) {
$matchFound = $false
foreach ($dev in $comparestrings) {
if ($dev -like "*$item*") {
$matchFound = $true
echo "Match found"
break
}
}
if (-not $matchFound) {
Write-Host "No match found for $item"
}
}
**I want to print the match found when sg-robot-platform matches with cn-sg-robot-platform pattern **
答案1
得分: 1
以下是翻译好的代码部分:
- 遍历
$comparestrings
数组并测试每个值是否存在匹配项
$groupslist = "cn-sg-robot-platform","cn-sg-capture-platform","cn-sg-easy-platform","cn-sg-content-platform"
$comparestrings = "sg-robot-platform", "sg-easy-platform"
foreach ($item in $comparestrings) {
if ($groupslist -like "*$item*") {
"找到匹配项:$item"
}
}
- 使用
Where-Object
并遍历结果
$groupslist = "cn-sg-robot-platform","cn-sg-capture-platform","cn-sg-easy-platform","cn-sg-content-platform"
$comparestrings = "sg-robot-platform", "sg-easy-platform"
$comparestrings | Where-Object { $groupslist -like "*$_*" } | ForEach-Object { "找到匹配项:$_" }
# 或者使用正则表达式 -match
# $comparestrings | Where-Object { $groupslist -match $_ } | ForEach-Object { "找到匹配项:$_" }
这两种方法都会输出:
找到匹配项:sg-robot-platform
找到匹配项:sg-easy-platform
英文:
Not quite sure what you intend to do here, but if you want to output where a match can be found, here's two possible solutions for you:
- loop over the $comparestrings array and test each value if there is a match
$groupslist = "cn-sg-robot-platform","cn-sg-capture-platform","cn-sg-easy-platform","cn-sg-content-platform"
$comparestrings = "sg-robot-platform" , "sg-easy-platform"
foreach ($item in $comparestrings) {
if ($groupslist -like "*$item*") {
"Match found for $item"
}
}
- use
Where-Object
and loop over the results
$groupslist = "cn-sg-robot-platform","cn-sg-capture-platform","cn-sg-easy-platform","cn-sg-content-platform"
$comparestrings = "sg-robot-platform" , "sg-easy-platform"
$comparestrings | Where-Object { $groupslist -like "*$item*" } | ForEach-Object { "Match found for $_" }
# or use regex -match
# $comparestrings | Where-Object { $groupslist -match $item } | ForEach-Object { "Match found for $_" }
Both will output
Match found for sg-robot-platform
Match found for sg-easy-platform
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论