Challenge - 5 Problems
Named Capture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of Named Capture Groups in PowerShell Regex
What is the output of this PowerShell script using named capture groups?
PowerShell
$text = 'User: Alice, Age: 30' if ($text -match '(?<name>User: \w+), Age: (?<age>\d+)') { "$($matches['name']) and $($matches['age'])" } else { 'No match' }
Attempts:
2 left
💡 Hint
Remember that named captures include the full matched text for the group name.
✗ Incorrect
The named capture 'name' captures 'User: Alice' including the label, and 'age' captures '30'. The script outputs both exactly as captured.
💻 Command Output
intermediate2:00remaining
Extracting Named Groups with Multiple Matches
What will be the output of this PowerShell script that uses named captures with multiple matches?
PowerShell
$text = 'ID:123 Name:John; ID:456 Name:Jane' $pattern = '(ID:(?<id>\d+)) Name:(?<name>\w+)' $matches = [regex]::Matches($text, $pattern) foreach ($m in $matches) { "$($m.Groups['id'].Value) - $($m.Groups['name'].Value)" }
Attempts:
2 left
💡 Hint
Look at how the named groups 'id' and 'name' are accessed from each match.
✗ Incorrect
The regex matches two groups: 'id' captures digits after 'ID:', and 'name' captures the word after 'Name:'. The foreach loop prints each pair separated by ' - '.
🔧 Debug
advanced2:00remaining
Identify the Error in Named Capture Usage
What error will this PowerShell script produce when run?
PowerShell
$text = 'Error 404: Not Found' if ($text -match '(?<code>\d+): (?<message>.+)') { Write-Output $matches['code'] Write-Output $matches['message'] } else { Write-Output 'No match' }
Attempts:
2 left
💡 Hint
Check if the regex pattern matches the input string exactly.
✗ Incorrect
The regex expects a colon ':' immediately after digits, but the input has 'Error 404: Not Found' with a space before digits. The match fails, so 'No match' is output.
🧠 Conceptual
advanced1:30remaining
Understanding Named Capture Group Behavior
In PowerShell, what does the named capture group store in the automatic $matches variable after a successful regex match?
Attempts:
2 left
💡 Hint
Think about what a capture group is designed to extract.
✗ Incorrect
Named capture groups store the substring matched inside their parentheses, accessible by the group name in $matches.
🚀 Application
expert3:00remaining
Extracting and Using Named Captures in a Script
Given the text 'Order #12345: Product=Book, Qty=3', which PowerShell script correctly extracts the order number, product name, and quantity using named captures and outputs them as JSON?
Attempts:
2 left
💡 Hint
Access named captures from $matches using the correct syntax and convert quantity to integer if needed.
✗ Incorrect
Option C correctly accesses named captures using $matches['name'] syntax and creates a PSCustomObject, then converts it to JSON. It matches PowerShell syntax and best practice.