Bird
0
0

How would you modify this switch statement to match strings that start with 'error' or 'warn' using regex, and print 'Alert' for both?

hard📝 Application Q9 of 15
PowerShell - Control Flow
How would you modify this switch statement to match strings that start with 'error' or 'warn' using regex, and print 'Alert' for both?
switch -Regex ($messages) {
  '^error' { 'Error found' }
  '^warn' { 'Warning found' }
  default { 'No alert' }
}
ACombine patterns using alternation: '^error|^warn' { 'Alert' }
BUse wildcard: 'error*' and 'warn*' with -Wildcard
CUse separate cases with same output as is
DReplace default with '^alert' pattern
Step-by-Step Solution
Solution:
  1. Step 1: Understand regex alternation

    Using '^error|^warn' matches strings starting with either 'error' or 'warn'.
  2. Step 2: Simplify switch cases

    Combining into one case reduces repetition and prints 'Alert' for both patterns.
  3. Final Answer:

    Combine patterns using alternation: '^error|^warn' { 'Alert' } -> Option A
  4. Quick Check:

    Regex alternation '|' combines patterns [OK]
Quick Trick: Use | in regex to combine multiple patterns [OK]
Common Mistakes:
  • Using wildcard patterns with -Regex
  • Duplicating cases instead of combining
  • Changing default to unrelated pattern

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes