Bird
0
0

What will this PowerShell script output?

medium📝 Command Output Q5 of 15
PowerShell - Regular Expressions
What will this PowerShell script output?
$data = 'ID: 1234; Status: Active'
if ($data -match 'ID: (?\d+); Status: (?\w+)') {
  "$($matches['status']) with ID $($matches['id'])"
} else {
  'No match found'
}
AActive with ID 1234
B1234 with ID Active
CNo match found
DError: Invalid syntax
Step-by-Step Solution
Solution:
  1. Step 1: Understand regex groups

    'id' captures digits after 'ID:', 'status' captures word after 'Status:'.
  2. Step 2: Match input string

    Input matches with id=1234 and status=Active.
  3. Final Answer:

    Active with ID 1234 -> Option A
  4. Quick Check:

    Named captures extract correct values = Active with ID 1234 [OK]
Quick Trick: Match groups in order and use $matches['name'] to access [OK]
Common Mistakes:
  • Swapping id and status values
  • Assuming no match due to semicolon
  • Using wrong group names

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes