A. Incorrect property access; should use $matches['user']
B. Regex pattern is invalid
C. Variable $text is not defined
D. Missing closing parenthesis in if statement
Solution
Step 1: Check regex and variable definitions
The regex and $text variable are correct and properly defined.
Step 2: Identify how to access named captures
In PowerShell, named captures are accessed as $matches['name'], not $matches.name.
Final Answer:
Incorrect property access; should use $matches['user'] -> Option A
Quick Check:
Access named capture with $matches['name'] [OK]
Hint: Use brackets $matches['name'], not dot notation [OK]
Common Mistakes:
Using dot notation for $matches named groups
Assuming regex is wrong
Ignoring variable initialization
5. You want to extract 'first' and 'last' names from a string like 'Name: Jane Doe' using named captures in PowerShell. Which script correctly captures and prints them as "Doe, Jane"?
hard
A.
$text = 'Name: Jane Doe'
if ($text -match 'Name: (?<last>\w+) (?<first>\w+)') {
"$($matches['last']), $($matches['first'])"
}
B.
$text = 'Name: Jane Doe'
if ($text -match 'Name: (?<first>\w+) (?<last>\w+)') {
"$($matches['last']), $($matches['first'])"
}
C.
$text = 'Name: Jane Doe'
if ($text -match 'Name: (\w+) (\w+)') {
"$($matches['last']), $($matches['first'])"
}
D.
$text = 'Name: Jane Doe'
if ($text -match 'Name: (?<first>\w+) (?<last>\w+)') {
"$($matches.first), $($matches.last)"
}
Solution
Step 1: Check regex named capture order
The pattern must capture first name then last name as named groups 'first' and 'last' respectively.
Step 2: Verify output formatting and access
Access named captures with $matches['name'] and print as "last, first".
$text = 'Name: Jane Doe'
if ($text -match 'Name: (?<first>\w+) (?<last>\w+)') {
"$($matches['last']), $($matches['first'])"
}
does this correctly.
Final Answer:
The script correctly captures and prints "Doe, Jane" -> Option B
Quick Check:
Correct named capture order and access =
$text = 'Name: Jane Doe'
if ($text -match 'Name: (?<first>\w+) (?<last>\w+)') {
"$($matches['last']), $($matches['first'])"
}
[OK]
Hint: Match names in order and use $matches['name'] to print [OK]
Common Mistakes:
Swapping first and last in regex
Using unnamed captures but accessing by name
Using dot notation for $matches
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.