Finding a string for an array of string fragments

I need to search by string to see if it contains any text in an array of strings. for instance

excludeList = "warning", "general unimportant thing", "something else"

searchString = here is a line telling us about a common non-essential thing.

otherString = something common but not related

In this example, we will find the string "common non-essential thing" from the array in my searchList and return true. however, otherString does not contain any of the complete lines in the array, therefore returns false.

I am sure it is not so difficult, but I have looked at it for too long ...

Update: The best I can do so far:

#list of excluded terms
$arrColors = "blue", "red", "green", "yellow", "white", "pink", "orange", "turquoise"

#the message of the event we've pulled
$testString = "there is a blue cow over there"
$test2="blue"
$count=0
#check if the message contains anything from the secondary list
$arrColors | ForEach-Object{
    echo $count
    echo $testString.Contains($arrColors[$count])
    $count++

}

he is not too elegant, though ...

+3
source share
2

. '|' OR:

PS> $excludeList="warning|a common unimportant thing|something else"
PS> $searchString="here is a string telling us about a common unimportant thing."
PS> $otherString="something common but unrelated"

PS> $searchString -match $excludeList
True

PS> $otherString -match $excludeList
False
+9

, , true, .

function ContainsAny( [string]$s, [string[]]$items ) {
  $matchingItems = @($items | where { $s.Contains( $_ ) })
  [bool]$matchingItems
}
+3

Source: https://habr.com/ru/post/1767057/


All Articles