Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

-like with multiple patterns

Is there an easy way (not using loops) to apply -like with more than one pattern to one string?

so

"c:\myfile.txt" -like "*.dat","*.txt"

should return $true

"c:\myfile.dat" -like "*.dat","*.txt"

should return $true

"c:\myfile.doc" -like ".dat",".txt"

should return $false

like image 312
mishkin Avatar asked Dec 05 '25 19:12

mishkin


1 Answers

I don't think so. You could use regular expression:

"c:\myfile.txt" -match '^.+\.(dat|txt)$'

UPDATE

Make a regex pattern from input that contains a wildcard patterns:

    PS> $or = '"*.dat","*.txt","*.foo"' -replace '"|\*\.'  -replace ',','|'
    PS> $pattern = '^.+({0})$' -f $or
    PS> $pattern 
    ^.+(dat|txt|foo)$

   $string -match $pattern
like image 92
Shay Levy Avatar answered Dec 08 '25 23:12

Shay Levy