Bird
0
0

Which of the following is the correct way to use the -match operator to check if the variable $message contains the substring "error"?

easy📝 Syntax Q3 of 15
PowerShell - Regular Expressions
Which of the following is the correct way to use the -match operator to check if the variable $message contains the substring "error"?
Aif ($message -contains "error") { ... }
Bif ($message match "error") { ... }
Cif ($message =~ "error") { ... }
Dif ($message -match "error") { ... }
Step-by-Step Solution
Solution:
  1. Step 1: Understand the operator

    The -match operator is used to test if a string matches a regex pattern.
  2. Step 2: Check syntax

    The correct syntax is if ($variable -match "pattern"). if ($message -match "error") { ... } uses this correctly.
  3. Final Answer:

    if ($message -match "error") { ... } -> Option D
  4. Quick Check:

    Correct operator and syntax used [OK]
Quick Trick: Use '-match' with variable and pattern in quotes [OK]
Common Mistakes:
  • Using '-contains' instead of '-match'
  • Using 'match' without a dash
  • Using '=~' which is not valid in PowerShell

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes