Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell match a single string against multiple regexs?

Tags:

Is there a more 'powershelly' way of matching a single string against an array/collection of regexes other than iterating through each one in turn?

What I'd really like to be able to do is something this

$database.Name -match $includeRegexArray

Given the way Powershell works it feels like there should be a nicer solution than writing a function to iterate over the array

like image 344
David Hayes Avatar asked Feb 01 '10 21:02

David Hayes


People also ask

How do I match a substring in PowerShell?

Wildcard character (*) matches zero or more characters in the string and if the input string is secular then it gives the boolean output and if it is a string array then the matching strings will be the output as shown below. In the above example, wildcard character (*) matches the string before it (case-insensitive).

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .

How do I match a pattern in PowerShell?

The period ( . ) is a wildcard character in regular expressions. It will match any character except a newline ( \n ). # This expression returns true. # The pattern matches any 4 characters except the newline.

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1. 1* means any number of ones.


1 Answers

Select-String will accept an array of regex patterns:

Select-String $includeRegexArray -inp $database.Name
like image 199
Keith Hill Avatar answered Oct 11 '22 13:10

Keith Hill