Bird
0
0

Identify the error in this PowerShell snippet using named captures:

medium📝 Debug Q14 of 15
PowerShell - Regular Expressions
Identify the error in this PowerShell snippet using named captures:
$text = 'User: JohnDoe'
if ($text -match '(?\w+)') {
  Write-Output $matches.user
}
AIncorrect property access; should use $matches['user']
BRegex pattern is invalid
CVariable $text is not defined
DMissing closing parenthesis in if statement
Step-by-Step Solution
Solution:
  1. Step 1: Check regex and variable definitions

    The regex and $text variable are correct and properly defined.
  2. Step 2: Identify how to access named captures

    In PowerShell, named captures are accessed as $matches['name'], not $matches.name.
  3. Final Answer:

    Incorrect property access; should use $matches['user'] -> Option A
  4. Quick Check:

    Access named capture with $matches['name'] [OK]
Quick Trick: Use brackets $matches['name'], not dot notation [OK]
Common Mistakes:
  • Using dot notation for $matches named groups
  • Assuming regex is wrong
  • Ignoring variable initialization

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes