Bird
0
0

What will be the output of this PowerShell code?

medium📝 Command Output Q4 of 15
PowerShell - Regular Expressions
What will be the output of this PowerShell code?
$text = 'User: JohnDoe, Age: 30'
if ($text -match '(?\w+), Age: (?\d+)') {
  "$($matches['name']) is $($matches['age']) years old"
} else {
  'No match'
}
ANo match
BJohnDoe is 30 years old
CUser is 30 years old
DError: Invalid regex
Step-by-Step Solution
Solution:
  1. Step 1: Analyze regex pattern

    The pattern captures a word before ', Age:' as 'name' and digits after as 'age'.
  2. Step 2: Match input string

    Input 'User: JohnDoe, Age: 30' matches 'JohnDoe' as name and '30' as age.
  3. Final Answer:

    JohnDoe is 30 years old -> Option B
  4. Quick Check:

    Named captures extract correct parts = JohnDoe is 30 years old [OK]
Quick Trick: Check regex groups match input parts exactly [OK]
Common Mistakes:
  • Matching 'User' instead of 'JohnDoe'
  • Assuming no match due to colon
  • Confusing group names with literal text

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes