Bird
0
0

Identify the error in this code:

medium📝 Debug Q6 of 15
PowerShell - String Operations
Identify the error in this code:
$text = 'abc123'
if ($text -match '\d+') { $match[0] }
ANo error; code runs fine
BVariable $match is incorrect; should be $Matches
CMissing else block
DRegex pattern is invalid
Step-by-Step Solution
Solution:
  1. Step 1: Check variable names for matches

    The automatic variable storing matches is $Matches, not $match.
  2. Step 2: Confirm regex and syntax

    The regex and if statement syntax are correct.
  3. Final Answer:

    Variable $match is incorrect; should be $Matches -> Option B
  4. Quick Check:

    Use $Matches, not $match [OK]
Quick Trick: Use $Matches, case-sensitive [OK]
Common Mistakes:
  • Using lowercase $match instead of $Matches
  • Assuming regex is wrong
  • Thinking else block is mandatory

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes