user1612851
user1612851

Reputation: 1214

Using contains and match at same time?

Is there a simple way to do this type of compare:

$s =  {"This is a dog" ,"This is a cat"}

$s | where {$_.Phrase -contains -match{"dog",cat","rat"} }

Basically, if the left contains any part of anything on the right, I want it returned.

Or do I have to do -or for each thing on the right?

Upvotes: 0

Views: 79

Answers (1)

zdan
zdan

Reputation: 29450

Assuming that $s is an array of strings (and not a scriptblock) you could create the appropriate regular expression for the -match operator and just use that:

# ~> $s =  @("This is a dog" ,"This is a cat", "this is neither")

# ~> $s | where {$_ -match "dog|cat|rat" }
This is a dog
This is a cat

Upvotes: 2

Related Questions