PowerShell Switch Statement with Regex Parameter


Example

The -Regex parameter allows switch statements to perform regular expression matching against conditions.

Example:

switch -Regex ('Condition')
{ 
  'Con\D+ion'    {'One or more non-digits'}
  'Conditio*$'   {'Zero or more "o"'} 
  'C.ndition'    {'Any single char.'}  
  '^C\w+ition$'  {'Anchors and one or more word chars.'} 
  'Test'         {'No match'} 
}

Output:

One or more non-digits
Any single char.
Anchors and one or more word chars.