0
0
PowerShellscripting~20 mins

Named captures in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Named Capture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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'
}
AUser: Alice and 30
BAlice and 30
CNo match
DUser: Alice, Age: 30
Attempts:
2 left
💡 Hint
Remember that named captures include the full matched text for the group name.
💻 Command Output
intermediate
2: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)"
}
ANo output
B
ID:123 - Name:John
ID:456 - Name:Jane
C
123 Name:John
456 Name:Jane
D
123 - John
456 - Jane
Attempts:
2 left
💡 Hint
Look at how the named groups 'id' and 'name' are accessed from each match.
🔧 Debug
advanced
2: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'
}
ANo match
B
404
Not Found
CRuntime error: Index operation failed
DSyntax error
Attempts:
2 left
💡 Hint
Check if the regex pattern matches the input string exactly.
🧠 Conceptual
advanced
1: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?
AThe entire input string matched by the regex
BOnly the text matched inside the parentheses of the named group
CThe index position of the match in the input string
DThe name of the group as a string
Attempts:
2 left
💡 Hint
Think about what a capture group is designed to extract.
🚀 Application
expert
3: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?
A
$text = 'Order #12345: Product=Book, Qty=3'
if ($text -match 'Order #(?&lt;order&gt;\d+): Product=(?&lt;product&gt;\w+), Qty=(?&lt;qty&gt;\d+)') {
  $obj = [PSCustomObject]@{
    OrderNumber = $matches.order
    ProductName = $matches.product
    Quantity = $matches.qty
  }
  $obj | ConvertTo-Json
}
B
$text = 'Order #12345: Product=Book, Qty=3'
if ($text -match 'Order #(?&lt;order&gt;\d+): Product=(?&lt;product&gt;\w+), Qty=(?&lt;qty&gt;\d+)') {
  $obj = [PSCustomObject]@{
    OrderNumber = $matches['order']
    ProductName = $matches['product']
    Quantity = [int]$matches['qty']
  }
  $obj | ConvertTo-Json
}
C
$text = 'Order #12345: Product=Book, Qty=3'
if ($text -match 'Order #(?&lt;order&gt;\d+): Product=(?&lt;product&gt;\w+), Qty=(?&lt;qty&gt;\d+)') {
  $obj = [PSCustomObject]@{
    OrderNumber = $matches['order']
    ProductName = $matches['product']
    Quantity = $matches['qty']
  }
  $obj | ConvertTo-Json
}
D
$text = 'Order #12345: Product=Book, Qty=3'
if ($text -match 'Order #(?&lt;order&gt;\d+): Product=(?&lt;product&gt;\w+), Qty=(?&lt;qty&gt;\d+)') {
  $obj = @{OrderNumber=$matches.order; ProductName=$matches.product; Quantity=$matches.qty}
  $obj | ConvertTo-Json
}
Attempts:
2 left
💡 Hint
Access named captures from $matches using the correct syntax and convert quantity to integer if needed.