Bird
Raised Fist0
PowerShellscripting~20 mins

Named captures in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does a named capture group do in a PowerShell regex pattern like (?<year>\d{4})?
easy
A. Matches only the first 4 characters of the string
B. Makes the regex case-insensitive
C. Repeats the pattern 4 times
D. Labels the matched part as 'year' for easy access later

Solution

  1. Step 1: Understand named capture syntax

    The syntax (?<name>pattern) labels the matched text as 'name'.
  2. Step 2: Identify the purpose in the example

    Here, (?<year>\d{4}) captures 4 digits and labels them 'year'.
  3. Final Answer:

    Labels the matched part as 'year' for easy access later -> Option D
  4. Quick Check:

    Named capture = label matched text [OK]
Hint: Look for (?<name>pattern) to label matches [OK]
Common Mistakes:
  • Thinking it changes match behavior
  • Confusing with quantifiers
  • Assuming it matches only start of string
2. Which of the following is the correct syntax to create a named capture group called 'day' matching two digits in PowerShell regex?
easy
A. (day:\d{2})
B. (?day:\d{2})
C. (?<day>\d{2})
D. (?P<day>\d{2})

Solution

  1. Step 1: Recall PowerShell named capture syntax

    PowerShell uses (?<name>pattern) for named captures.
  2. Step 2: Match the correct option

    (?<day>\d{2}) uses (?<day>\d{2}), which is correct syntax for two digits named 'day'.
  3. Final Answer:

    (?<day>\d{2}) -> Option C
  4. Quick Check:

    PowerShell named capture = (?<name>pattern) [OK]
Hint: Use (?<name>pattern) for named groups in PowerShell [OK]
Common Mistakes:
  • Using Python or other language syntax
  • Omitting the question mark
  • Using colon instead of angle brackets
3. What will be the output of this PowerShell code?
$text = 'Date: 2023-07-15'
if ($text -match '(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})') {
  "$($matches['year'])/$($matches['month'])/$($matches['day'])"
} else {
  'No match'
}
medium
A. "2023/07/15"
B. "Date: 2023-07-15"
C. "No match"
D. "2023-07-15"

Solution

  1. Step 1: Understand the regex and matching

    The regex captures year, month, and day as named groups from the string 'Date: 2023-07-15'.
  2. Step 2: Access named captures in $matches

    After matching, $matches['year'] = '2023', $matches['month'] = '07', $matches['day'] = '15'. The output formats them as "2023/07/15".
  3. Final Answer:

    "2023/07/15" -> Option A
  4. Quick Check:

    Named captures accessed = formatted date [OK]
Hint: Use $matches['name'] to get named group values [OK]
Common Mistakes:
  • Expecting original string output
  • Using $matches.year instead of $matches['year']
  • Not matching regex correctly
4. Identify the error in this PowerShell snippet using named captures:
$text = 'User: JohnDoe'
if ($text -match '(?<user>\w+)') {
  Write-Output $matches.user
}
medium
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

  1. Step 1: Check regex and variable definitions

    The regex and $text variable are correct and properly defined.
  2. Step 2: Identify how to access named captures

    In PowerShell, named captures are accessed as $matches['name'], not $matches.name.
  3. Final Answer:

    Incorrect property access; should use $matches['user'] -> Option A
  4. 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

  1. Step 1: Check regex named capture order

    The pattern must capture first name then last name as named groups 'first' and 'last' respectively.
  2. 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.
  3. Final Answer:

    The script correctly captures and prints "Doe, Jane" -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard